python 計算元素出現的次數

  • 2019 年 10 月 7 日
  • 筆記

collections.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' ] from collections import Counter word_counts = Counter(words)

出現頻率最高的3個單詞

top_three = word_counts.most_common(3) print(top_three)

Outputs [('eyes', 8), ('the', 5), ('look', 4)]