快速的统计一个字典里面每个元素出现的次数
import collections
most_common = collections.Counter(['a', 'a', 'b' ]).most_common()
print(most_common)
[('a', 2), ('b', 1)]
注意上面的参数必须是list类型的,不能是二维的,或者其他维度的
比如下面的代码因为维度不对,就会报错
a = [
[1, 2, 3],
[4, 5, 6]
]
most_common = collections.Counter(a).most_common()
print(most_common)
TypeError: unhashable type: 'list'
最后
以上就是暴躁香烟最近收集整理的关于collections.Counter的全部内容,更多相关collections内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复