1.这篇课文是《背影》,来统计其中词语出现的频率
import jieba
txt = open("D:/用户目录/下载/背影.txt", "r", encoding='utf-8').read()
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(15):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))
----------------------------------------------------------------------------------------------------------------
大哥 58
时候 38
什么 33
我们 32
知道 30
他们 25
老爷子 21
一个 18
没有 18
嫂子 18
起来 16
就是 16
怎么样 15
家里 15
爷爷 15
--------------------------------------------------------------------------------------------------------------