函数的定义和常用操作 && 课堂练习

Python笔记本
493 0

代码

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
评论 ( 0 )
评论已关闭