西游记 词频统计

 1 import jieba
 2 txt=open("D://python_common_exercise//west_journey.txt",'r',encoding="utf-8").read()
 3 words=jieba.lcut(txt)   #jieba库函数
 4 count={}    #创建字典
 5 for word in words:
 6     if len(word)==1:
 7         continue
 8     else:
 9         count[word]=count.get(word,0)+1
10 items=list(count.items())   #转换成列表
11 items.sort(key=lambda x:x[-1],reverse=True)
12 for i in range(15):
13     word,count=items[i]
14     print("{0:<10}{1:>5}".format(word,count))

猜你喜欢

转载自www.cnblogs.com/hrunjie/p/12638185.html