首页 > 代码库 > [PY3]——找出一个序列中出现次数最多的元素/collections.Counter 类的用法
[PY3]——找出一个序列中出现次数最多的元素/collections.Counter 类的用法
问题
怎样找出一个序列中出现次数最多的元素呢?
解决方案
collections.Counter
类就是专门为这类问题而设计的, 它甚至有一个有用的 most_common()
方法直接给了你答案
collections.Counter 类
1. most_common(n)统计top_n
from collections import Counterwords = [ ‘look‘, ‘into‘, ‘my‘, ‘eyes‘, ‘look‘, ‘into‘, ‘my‘, ‘eyes‘, ‘the‘, ‘eyes‘, ‘the‘, ‘eyes‘, ‘the‘, ‘eyes‘, ‘not‘, ‘around‘, ‘the‘, ‘eyes‘, "don‘t", ‘look‘, ‘around‘, ‘the‘, ‘eyes‘, ‘look‘, ‘into‘, ‘my‘, ‘eyes‘, "you‘re", ‘under‘]#创建Counter对象word_counts=Counter(words)#most_common(n)函数可直接统计top-ntop_three=word_counts.most_common(3)print(type(top_three))<class ‘list‘>print(top_three)[(‘eyes‘, 8), (‘the‘, 5), (‘look‘, 4)]
2. 统计任意元素出现的次数
Counter
对象可以接受任意的由可哈希(hashable
)元素构成的序列对象
在底层实现上,一个 Counter
对象就是一个字典,将元素映射到它出现的次数上
print(word_counts[‘look‘])4print(word_counts["you‘re"])1
3. 两个Counter对象之间可以互相使用数学运算符,从而叠加/叠减统计
word1 = [ ‘look‘, ‘into‘, ‘my‘, ‘eyes‘, ‘look‘, ‘into‘, ‘my‘, ‘eyes‘, ‘the‘, ‘eyes‘, ‘the‘, ‘eyes‘, ‘the‘, ‘eyes‘, ‘not‘, ‘around‘, ‘the‘, ‘eyes‘, "don‘t", ‘look‘, ‘around‘, ‘the‘, ‘eyes‘, ‘look‘, ‘into‘, ‘my‘, ‘eyes‘, "you‘re", ‘under‘]word2 = [‘why‘,‘are‘,‘you‘,‘not‘,‘looking‘,‘in‘,‘my‘,‘eyes‘]a=Counter(word1)b=Counter(word2)print(a)Counter({‘eyes‘: 8, ‘the‘: 5, ‘look‘: 4, ‘into‘: 3, ‘my‘: 3, ‘around‘: 2, ‘not‘: 1, ‘under‘: 1, "you‘re": 1, "don‘t": 1})print(b)Counter({‘eyes‘: 1, ‘not‘: 1, ‘are‘: 1, ‘you‘: 1, ‘in‘: 1, ‘why‘: 1, ‘my‘: 1, ‘looking‘: 1})print(a+b)Counter({‘eyes‘: 9, ‘the‘: 5, ‘my‘: 4, ‘look‘: 4, ‘into‘: 3, ‘around‘: 2, ‘not‘: 2, ‘under‘: 1, ‘looking‘: 1, ‘you‘: 1, ‘why‘: 1, ‘in‘: 1, ‘are‘: 1, "you‘re": 1, "don‘t": 1})print(a-b)Counter({‘eyes‘: 7, ‘the‘: 5, ‘look‘: 4, ‘into‘: 3, ‘around‘: 2, ‘my‘: 2, ‘under‘: 1, "you‘re": 1, "don‘t": 1})
[PY3]——找出一个序列中出现次数最多的元素/collections.Counter 类的用法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。