python实例 三国人物出场次序 jieba库

#Cal3kingdoms.py
import jieba
txt = open("threekingdoms.txt", "r", encoding="utf-8").read() # 文件的打开
exclude = {"将军","却说", "荆州"} #多次运行取出非人名词汇
words = jieba.lcut(txt) #利用jieba库分词 构造一个列表

counts = {} # 构造一个字典
for word in words: # 逐个遍历 对重名进行关联
  if len(word) == 1:
    continue
  elif word == "诸葛亮" or word == "孔明曰":
    rword = "孔明"
  elif word == "关公" or word == "云长":
    rword = "关羽"
  elif word == "玄德" or word == "玄德曰":
    rword = "刘备"
  elif word == "孟德" or word == "丞相曰":
    rword = "曹操"
  else:
    rword = word
  conuts[rword] = conuts.get(rword, 0) + 1 # get方法为若rword存在则取其值 否则取默认值0

for word in excludes: # 排除非人名
  del counts[word]
  
items = list(counts.items()) #将counts里items转为list
items.sort(key=lambda x: x[1], reverse=True) #利用list里sort排序

for i in range(10):
  word, count = items[i]
  print("{0:<10}{1:>5}".format(word, count))
原文地址:https://www.cnblogs.com/coderzjz/p/12738423.html