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

如何在Python中找出列表内文件名的第一与第二高分

获取文件名列表中的第二高分方案

首先修正原代码的关键问题:你当前提取的分数是字符串类型,直接做大小比较会出错(比如字符串"9"会被判定为大于"10"),必须先转换为整数int(img.split("_")[1])。

下面提供两种可靠的实现方式:

方法一:单次遍历跟踪前两名(高效)

只遍历一次列表,同时维护最高分和第二高分的数值,适合数据量较大的场景:

lst = ['2022-12-08 21:53:28.230556_54_12748.png', '2022-12-08 21:53:28.314088_86_14029.png', '2022-12-08 21:53:28.363581_72_12540.png', '2022-12-08 21:53:28.448268_65_13491.png', '2022-12-08 21:53:28.907252_50_7001.png', '2022-12-08 21:53:28.998206_55_13458.png', '2022-12-08 21:53:29.066905_60_8857.png', '2022-12-08 21:53:29.167242_54_8327.png', '2022-12-08 21:53:29.260880_52_11163.png', '2022-12-08 21:53:29.318770_52_7859.png', '2022-12-08 21:53:29.992619_51_7173.png', '2022-12-08 21:53:30.380722_55_7301.png', '2022-12-08 21:53:31.546448_59_7005.png', '2022-12-08 21:53:31.820877_53_6044.png', '2022-12-08 21:53:31.901512_51_5818.png', '2022-12-08 21:53:35.622028_56_7221.png', '2022-12-08 21:53:35.706456_72_7522.png', '2022-12-08 21:53:36.785644_53_8222.png', '2022-12-08 21:53:36.872902_60_10873.png', '2022-12-08 21:53:36.952308_59_10217.png', '2022-12-08 21:53:37.034774_57_7258.png', '2022-12-08 21:53:37.151830_52_7745.png', '2022-12-08 21:53:37.236664_80_13584.png', '2022-12-08 21:53:37.401254_70_13281.png', '2022-12-08 21:53:37.485339_91_16176.png', '2022-12-08 21:53:37.564524_74_13113.png', '2022-12-08 21:53:37.652097_90_13526.png', '2022-12-08 21:53:37.729431_82_13303.png', '2022-12-08 21:53:37.777823_66_12084.png', '2022-12-08 21:53:37.857692_72_14855.png', '2022-12-08 21:53:37.939566_72_16755.png', '2022-12-08 21:53:38.017759_65_13656.png', '2022-12-08 21:53:38.098182_59_14190.png', '2022-12-08 21:53:38.180636_55_13150.png', '2022-12-08 21:53:38.261705_54_11044.png', '2022-12-08 21:53:38.346376_67_11908.png', '2022-12-08 21:53:38.586157_59_11200.png', '2022-12-08 21:53:38.715544_64_10425.png', '2022-12-08 21:53:38.837041_64_12068.png', '2022-12-08 21:53:38.924294_77_13531.png', '2022-12-08 21:53:39.008601_60_10283.png', '2022-12-08 21:53:39.174924_60_10160.png', '2022-12-08 21:53:39.256190_65_10555.png', '2022-12-08 21:53:39.340980_63_10775.png']

# 初始化最高分和第二高分,设为-1(假设分数都是非负整数)
first_high = -1
second_high = -1

for img in lst:
    score = int(img.split("_")[1])
    if score > first_high:
        # 当前分数超过最高分,更新第二高分为原最高分,再更新最高分
        second_high = first_high
        first_high = score
    elif score > second_high and score != first_high:
        # 当前分数低于最高分,但高于第二高分,且不等于最高分(避免重复最高分干扰)
        second_high = score

print(f"最高分: {first_high}")
print(f"第二高分: {second_high}")

方法二:提取分数后去重排序(简洁)

先提取所有分数,去重后排序,直接取倒数第二个元素作为第二高分,适合数据量较小的场景:

lst = ['2022-12-08 21:53:28.230556_54_12748.png', ...] # 原列表

# 提取所有分数并转换为整数
scores = [int(img.split("_")[1]) for img in lst]
# 去重后升序排序
unique_sorted = sorted(set(scores))

# 处理所有分数相同的情况
if len(unique_sorted) >= 2:
    first_high = unique_sorted[-1]
    second_high = unique_sorted[-2]
else:
    first_high = unique_sorted[0]
    second_high = "所有分数均相同"

print(f"最高分: {first_high}")
print(f"第二高分: {second_high}")

注意事项

  • 必须将分数转换为整数后再比较,避免字符串比较的逻辑错误。
  • 如果列表中存在多个相同的最高分,两种方法都会自动忽略重复值,正确保留第二高分。

内容的提问来源于stack exchange,提问作者S Andrew

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 09:00:58