记录一下看过视频的实例:
统计文本文件中出现频率前20的词语
import jieba
f=open("绝对路径.txt","rb")
txt=f.read()
words=jieba.lcut(txt)#使用jieba库自带的方法
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 items[1],reverse=True)
for i in range(20):
word,count=items[i]
print("{0:<10}{1:>5}".format(word,count))