统计单词出现的字数

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)]


原文地址:https://www.cnblogs.com/misswjr/p/9387817.html