使用jieba库寻找相同词
一、工具
先安装好jieba库

下载好你需要的txt文件

(注:文件必须改为utf-8编码)
二、代码
import jieba
txt = open(r'D:\\三国演义 (2).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)
for i in range(10):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))
三、运行结果![]()
posted on 2019-04-03 23:21 shinawear-- 阅读(478) 评论(0) 收藏 举报

浙公网安备 33010602011771号