Skip to main content
 首页 » 编程设计

python之计算表格中的单词总数

2025年05月04日21emanlee

如果我有一个文本文件,包含:

Alex is a footballer 
Alex loves football 

在将文本文件读入 python 列表后,它输出:

lst = [['Alex','is','a','footballer'],['Alex','loves','football']] 

我如何计算列表中的单词数?我在想类似的事情:

count = 0 
for word in lst: 
    count += len(lst) 

输出:

Total number of words: 7 

请您参考如下方法:

你也可以做 sum(map(len,lst))。更简洁。