pythonのCounter.most_common()
4298 ワード
ソース:実行クラスコード-CountTxtClass.py–関数plot_sample_num
栗:シーケンスの中で最も多く現れる要素を見つけます
結果:
学習リンク:
PythonのCountermost_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)
# 3
top_three = word_counts.most_common(3)
print(top_three)
結果:
[('eyes', 8), ('the', 5), ('look', 4)]
学習リンク:
PythonのCountermost_common()メソッド