作业14

import jieba
txt = open("D:\\浏览器下载\\西游记.txt","r",encoding = "utf-8").read()                  #打开文本
words = jieba.lcut(txt)           #分词
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 x:x[1],reverse=True)    #排序。其中lambda x:x[1] 即将词频次数进行排序(Ture,从大到小)
elem = []
for i in range(20):
    word ,count = items[i]
    elem.append(word)
    print("{:<10}{:>5}".format(word,count))       

 

posted @ 2020-11-15 10:42  减辞  阅读(39)  评论(0)    收藏  举报