問題
怎樣找出一個序列中出現次數最多的元素呢?
解決方案
collections.Counter
類就是專門為這類問題而設計的, 它甚至有一個有用的 most_common()
方法直接給了你答案
collections.Counter 類
1. most_common(n)統計top_n
from collections import 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' ] #建立Counter物件 word_counts=Counter(words) #most_common(n)函式可直接統計top-n top_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']) 4 print(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})