如何循环统计得分最高的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
相关产品推荐
相关产品推荐

