确定序列中出现频率最高的项

collections模块有一个类,专门用于统计序列中各个元素出现的次数,叫做Counter。

Counter实际上是dict的一个子类。并提供了额外的一些专门用于统计次数的方法:most_common()。

words = [
    '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'
]
from collections import Counter 
word_counts = Counter(words)
top_three = word_counts.most_common(3) 
print(top_three)
# Outputs [('eyes', 8), ('the', 5), ('look', 4)]

Counter对象的一个很少被知道的特性是其实现了对象的相关数学操作,如下:

words = [
    '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'
]
morewords = ['why','are','you','not','looking','in','my','eyes']
>>> a = Counter(words)
>>> b = Counter(morewords)
>>> a
Counter({'eyes': 8, 'the': 5, 'look': 4, 'into': 3, 'my': 3, 'around': 2,
             "you're": 1, "don't": 1, 'under': 1, 'not': 1})
>>> b
Counter({'eyes': 1, 'looking': 1, 'are': 1, 'in': 1, 'not': 1, 'you': 1,
             'my': 1, 'why': 1})
>>> # Combine counts
>>> c = a + b
>>> c
Counter({'eyes': 9, 'the': 5, 'look': 4, 'my': 4, 'into': 3, 'not': 2,
             'around': 2, "you're": 1, "don't": 1, 'in': 1, 'why': 1,
             'looking': 1, 'are': 1, 'under': 1, 'you': 1})
>>> # Subtract counts
>>> d = a - b
>>> d
Counter({'eyes': 7, 'the': 5, 'look': 4, 'into': 3, 'my': 2, 'around': 2,
             "you're": 1, "don't": 1, 'under': 1})
>>>

毋庸置疑,Counter对象对于几乎任何需要制表和计数数据的问题都是非常有用的工具。 相比涉及字典的手动操作的解决方案,你应该优先考虑使用Counter对象。

posted @ 2019-08-04 22:26  Jeffrey_Yang  阅读(246)  评论(0编辑  收藏  举报