统计单词出现的字数
2018-07-29 21:46 misswjr 阅读(347) 评论(0) 编辑 收藏 举报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'
# ]
with open('d:/py/3/counttimes.txt') as file1: # 打开文本文件
str1 = file1.read().split(' ') # 将文章按照空格划分开
word_counts = Counter(str1)
# 出现频率最高的3个单词
top_three = word_counts.most_common(3)
print(top_three)
# Outputs [('eyes', 8), ('the', 5), ('look', 4)]