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

赛道数据分析:无if条件判断的赛车排名实现方案求助

赛车获胜者判定优化方案

核心思路

先将所有赛车的圈数、最佳圈速、最后圈速结构化存储,再通过多条件排序规则自动完成排名判定,彻底避免冗余的if判断。

具体实现步骤

  1. 结构化赛车数据
    把分散的单变量数据整合为字典列表,每一条数据对应一辆赛车的完整信息:

    # 替换为你的实际变量值
    cars = [
        {"id": 1, "lap_counter": Lapcounter1, "best_lap": BestLap1, "last_lap": LastLap1},
        {"id": 2, "lap_counter": Lapcounter2, "best_lap": BestLap2, "last_lap": LastLap2},
        {"id": 3, "lap_counter": Lapcounter3, "best_lap": BestLap3, "last_lap": LastLap3},
        {"id": 4, "lap_counter": Lapcounter4, "best_lap": BestLap4, "last_lap": LastLap4}
    ]
    
  2. 定义多维度排序规则
    按照赛事排名逻辑(优先圈数多的;圈数相同则最佳圈速更快的;仍相同则最后圈速更快的),用sorted()函数的key参数实现自动排序:

    # 排序逻辑:-lap_counter 表示圈数降序,best_lap/last_lap 表示时间升序(更快)
    sorted_cars = sorted(cars, key=lambda x: (-x["lap_counter"], x["best_lap"], x["last_lap"]))
    
  3. 输出排名(支持并列)
    遍历排序后的列表,自动处理并列排名场景:

    current_rank = 1
    prev_score = None
    
    for idx, car in enumerate(sorted_cars):
        # 生成用于判断并列的成绩标识
        current_score = (car["lap_counter"], car["best_lap"], car["last_lap"])
        # 若当前车与上一辆成绩不同,更新排名
        if idx > 0 and current_score != prev_score:
            current_rank = idx + 1
        print(f"Car {car['id']} is ranked {current_rank}")
        prev_score = current_score
    

方案优势

  • 彻底消除冗余if判断,代码简洁可维护
  • 天然支持圈数相同的场景,无需额外修改逻辑
  • 可扩展性强:新增赛车或排名维度时,仅需修改数据结构或排序key即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 03:21:31