对我来说,您所做的工作看起来不错,您也可以使用collections.Counter(假设您使用的是python
2.7或更高版本)来获取更多信息,例如每个单词的数量。我的解决方案看起来像这样,可能会有一些改进。
import sysfrom collections import Counterlines = open(sys.argv[1], 'r').readlines()c = Counter()for line in lines: for work in line.strip().split(): c.update(work)for ind in c: print ind, c[ind]



