划句顾 发表于 2021-6-8 08:41:20

Python:统计《三国演义-三英战吕布》中出场最多的前20位人物。

'''*************************************************************************
    @author:LaoGu
    @time: 2021/6/8
    @fuction:统计《三国演义-三英战吕布》中出场最多的前20位人物。
****************************************************************************'''
import jieba
txt = open("三国演义-三英战吕布.txt","r").read()
words = jieba._lcut(txt)
counts = {}
for word in words:
    if len(word) == 1:
      continue
    else:
      counts = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key = lambda x : x,reverse = True)
print("《三国演义》中出场最多的前20位人物如下:")
for i in range(20):
    word,count = items
    print("{0:>40}".format(word))

结果如下:
页: [1]
查看完整版本: Python:统计《三国演义-三英战吕布》中出场最多的前20位人物。