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

如何循环统计得分最高的playerid并获取其总胜场与总败场数

实现代码及说明

统计规则

  • Pxxx = 玩家ID
  • 1 = 获胜(得分+1)
  • 0 = 落败(得分-1)
  • -1 = 未参赛(得分0)
  • 503 = 对局未结束(得分0)

完整实现代码

player_score = [['P001', '1', '0', '-1', '503', '1'], ['P067', '1', '1', '0', '-1', '503'], ['P218', '0', '1', '1', '-1', '-1'], ['P101', '0', '0', '1', '1', '503'], ['P456', '1', '1', '-1', '1', '-1']]
scores_table = {"1": 1, "0": -1}

player_stats = []
for p_data in player_score:
    pid = p_data[0]
    match_records = p_data[1:]
    win = match_records.count("1")
    lose = match_records.count("0")
    total_score = win * scores_table["1"] + lose * scores_table["0"]
    player_stats.append((pid, total_score, win, lose))

top_p = max(player_stats, key=lambda x: x[1])
# 英文输出(符合原题预期)
print(f"The top player is {top_p[0]} with {top_p[2]} wins and {top_p[3]} loses.")
# 中文输出(可替换上面的print语句)
# print(f"最高得分玩家为{top_p[0]},共取得{top_p[2]}次胜利,{top_p[3]}次失败。")

逻辑说明

  • 遍历每个玩家的对局记录,提前统计胜场、败场、总得分三个核心指标,避免后续重复计算
  • 取最高得分玩家时直接从预统计的列表中取值,可同时拿到所有需要输出的字段
  • 运行后输出结果与题目要求的预期完全一致

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 21:54:05