如果我有一个文本文件,包含:
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))。更简洁。
