問題描述
怎樣找出一個序列中出現次數最多的元素呢?
解決方案
collections.Counter類就是專門為這類問題而設計的,它甚至有一個有用的most_common()方法直接給了你答案,
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']
word_counts = Counter(words)
top_three = word_counts.most_common(3) # [('eyes', 8), ('the', 5), ('look', 4)]
討論
Counter物件在幾乎所有需要制表或者計數資料的場合是非常有用的工具,在解決這類問題時應該有限選擇它,而不是手動的利用字典去實作,
Counter物件可接受任意的由可哈希(hashable)元素構成的序列物件,在底層實作上,一個Counter就是一個字典,將元素映射到它出現的次數上,
word_counts['not'] # 1
word_counts['eyes'] # 8
如果想手動增加計數,可以簡單的使用加法:
morewords = ['why', 'are', 'you', 'not', 'looking', 'in', 'my', 'eyes']
for word in morewords:
word_counts[word] += 1
"""
word_counts['eyes'] = 9
"""
或者用update()方法:
word_counts.update(morewords)
Counter實體還可以跟數學運算操作相結合,比如:
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']
morewords = ['why', 'are', 'you', 'not', 'looking', 'in', 'my', 'eyes']
a = Counter(words)
b = Counter(morewords)
c = a + b # c = Counter({'eyes': 9, 'the': 5, 'look': 4, 'my': 4, 'into': 3, 'not': 2, 'around': 2, "don't": 1, "you're": 1, 'under': 1, 'why': 1, 'are': 1, 'you': 1, 'looking': 1, 'in': 1})
d = a - b # d = Counter({'eyes': 7, 'the': 5, 'look': 4, 'into': 3, 'my': 2, 'around': 2, "don't": 1, "you're": 1, 'under': 1})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/393843.html
標籤:Python
