代码
import re
def find_item( hero ):
with open('sanguo.txt',encoding='GB18030') as f:
data = f.read().replace('\n','')
name_num=len(re.findall(hero,data))
return name_num
#读取人物信息
name_dit = {}
with open('name.txt',encoding='utf-8') as f:
for line in f:
names = line.split('|')
for n in names:
name_num = find_item(n)
name_dit[n] = name_num
name_sorted = sorted(name_dit.items(), key=lambda item: item[1], reverse=True)
print(name_sorted[0:10])