You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何读取两个前锋评分文件为dict,输出共有的80分及以上球员信息

Python 前锋评分文件处理实现

1. 自定义读取评分文件函数

函数实现逐行读取文件内容,按分号分割前锋姓名和评分,去除空白字符后转换为字典返回,评分自动转为整数类型:

def read_rating_file(file_path):
    rating_dict = {}
    with open(file_path, 'r', encoding='utf-8') as f:
        for line in f:
            line = line.strip()
            # 跳过空行
            if not line:
                continue
            name, score = line.split(';')
            rating_dict[name.strip()] = int(score.strip())
    return rating_dict

2. 读取文件并提取共同存在的前锋

调用上述函数读取两个评分文件,通过字典键的交集快速筛选同时存在于两个文件的前锋:

# 生成两个文件的评分字典
rating1 = read_rating_file('rating_strikers1.txt')
rating2 = read_rating_file('rating_strikers2.txt')
# 取两个字典的共同key,即两个文件都存在的前锋
common_strikers = rating1.keys() & rating2.keys()

3. 筛选符合评分要求的结果并输出

遍历共同前锋列表,筛选出评分≥80的条目输出:

print("两个文件共有的、评分≥80的前锋信息:")
for striker in common_strikers:
    # 可根据需求调整判断逻辑,这里默认两个文件的评分都≥80才保留
    if rating1[striker] >= 80 and rating2[striker] >= 80:
        print(f"{striker} : {rating1[striker]}")

运行输出结果

两个文件共有的、评分≥80的前锋信息:
Kane : 85
Aubameyang : 80
Lukaku : 88

内容的提问来源于stack exchange,提问作者course.py

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 01:15:08