代码

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])

运行

相关文件

name.txt

sanguo.txt

weapon.txt

最后修改:2019 年 09 月 18 日
如果觉得我的文章对你有用,请随意赞赏