对于一个列表,要统计出这个列表里的所有元素个数,可以使用 collections.Counter 这个模块。
以下是使用示例:

直接将一个列表作为参数放入 Counter 即可。
from collections import Counter
words = ['a', 'b', 'v', 't', 'a', 'g', 'g', 'c', 'v', 'v', 'a', 'f']
result = Counter(words)
已经得到统计的结果了,现在想要找出这个字典里计数最多的几个,可以使用 most_common() 函数来得到结果:
result.most_common(3)
Out[7]: [('a', 3), ('v', 3), ('g', 2)]
直接对结果的某个元素进行更新:
result['a'] += 1
通过某个列表对原有的 Counter 进行更新:
words2 = ['a', 'c', 'e', 'a', 'c', 'v', 'v']
result.update(words2)