码迷,mamicode.com
首页 > 其他好文 > 详细

jieba:西游记

时间:2020-11-19 12:43:23      阅读:12      评论:0      收藏:0      [点我收藏+]

标签:tin   item   cut   键值   orm   class   rev   reverse   sort   

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    # 遍历所有词语,每出现一次其对应的值加 1
        
items = list(counts.items())#将键值对转换成列表
items.sort(key=lambda x: x[1], reverse=True)    # 根据词语出现的次数进行从大到小排序

for i in range(20):
    word, count = items[i]
    print("{0:<5}{1:>5}".format(word, count))

技术图片

jieba:西游记

标签:tin   item   cut   键值   orm   class   rev   reverse   sort   

原文地址:https://www.cnblogs.com/yuming1104/p/13973733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!