下载小说:
代码:
import jieba
def getWords():
txt = open('3guo1.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
word_list = list(counts.items())
word_list.sort(key = lambda x : x[1], reverse = True)
for i in range(22):
print(word_list[i])
return word_list
getWords()
for i in range(11):
print(l[i])
import jieba
def getWords():
txt = open('3guo1.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
word_list = list(counts.items())
word_list.sort(key = lambda x : x[1], reverse = True)
for i in range(22):
print(word_list[i])
return word_list
getWords()
for i in range(11):
print(l[i])
结果:
('曹操', 901)
('孔明', 853)
('将军', 749)
('却说', 642)
('玄德', 581)
('关公', 499)
('丞相', 471)
('二人', 455)
('不可', 424)
('荆州', 407)
('不能', 378)
('如此', 362)
('玄德曰', 358)
('孔明曰', 343)
('张飞', 342)
('商议', 332)
('如何', 326)
('主公', 316)
('军士', 298)
('吕布', 296)
('左右', 282)
('军马', 281)

