代码点读机 / Python / collections.Counter

collections.Counter Python · 模块功能

自动统计元素出现次数的字典。

怎么用

from collections import Counter
Counter(iterable)

小例子

from collections import Counter
print(Counter('banana'))

运行结果

Counter({'a': 3, 'n': 2, 'b': 1})

提醒:most_common 可排序。

官方文档:https://docs.python.org/zh-cn/3/library/collections.html#collections.Counter

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条