代码:
import jieba
print("====学号:202031013040====")
txt=open("聊斋志异.txt","r",encoding="gb18030")
words=jieba.lcut(txt.read())
counts={}
for word in words:
if len (word)==1:
continue
else:
counts[word]=counts.get(word,0)+1
item=list(counts.items())
item.sort(key=lambda x:x[1],reverse=True)
for i in range(20):
print("{0:<10}{1:>5}".format(item[i][0],item[i][1]))
运行结果: