import jieba
s=open(‘xiaoshuo.txt‘,‘r‘,encoding=‘utf-8‘)
#读入待分析的字符串
str=s.read()
s.close()
for i in ‘,。!、 \n “ ” ;‘:
str=str.replace(i,‘‘)
words=jieba.cut(str)
word=set(words)
#计数字典
dic={}
for i in word:
if len(i)>1:
dic[i]=str.count(i)
str=list(dic.items())
#排序
str.sort(key=lambda x:x[1],reverse=True)
for i in range(20):
print(str[i])