jieba 统计红楼梦人物出现次数

import jieba
with open('红楼梦.txt','r',encoding='utf-8')as f:
txt = f.read()
words = jieba.lcut(txt)
counts={}
for word in words:
if len(word) == 1:
continue
else:
counts[word]=counts.get(word,0)+1
list = list(counts.items())
list.sort(key=lambda x:x[1],reverse=True)
for i in range(20):
print("红楼梦出现第{}多的词语是{},出现的次数为{}".format(i+1

 

,list[i][0],list[i][1]))

posted @ 2023-12-28 21:55  一只蜗牛hhhh  阅读(110)  评论(0编辑  收藏  举报