most_common()函数,统计出现的次数
复制代码
1
2
3
4
5
6
7
8import collections a = ['你好', '你好', '中国', '华为'] b = collections.Counter(a).most_common(3) print(b)
输出:
[('你好', 2), ('中国', 1), ('华为', 1)]
计数值的访问与缺失的键
当所访问的键不存在时,返回0,而不是KeyError;否则返回它的计数。
复制代码
1
2
3a = ['你好', '你好', '中国', '华为'] e = collections.Counter(a) print(e['你好'])
输出:
2
最后
以上就是俏皮衬衫最近收集整理的关于Counter() most_common()统计的全部内容,更多相关Counter()内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复