Files
python3-cookbook/cookbook/c01/p12_mostfreq_items.py
2014-09-02 15:28:01 +08:00

26 lines
680 B
Python

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
Topic: 查找出现次数最多的元素
Desc :
"""
def most_freqency():
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)]
if __name__ == '__main__':
most_freqency()