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

如何遍历二维list统计各game_id的游玩、胜场、负场次数并存入字典

游戏对局数据统计实现方案

已知数据结构

game_record = [['*', 'G02', 'G05', 'G07', 'G08', 'G10'], 
          ['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']]

统计规则说明

  • 仅值为'1'(玩家参与并获胜)、'0'(玩家参与并落败)的记录计入统计
  • -1代表玩家未参与游戏、503代表游戏仍在进行中,两类记录均不计入统计
  • 每个游戏ID对应统计值格式为:[已完成总游玩次数, 胜场次数, 负场次数]

实现代码

# 初始化结果字典,初始值[总场次, 胜场数, 负场数]默认为[0,0,0]
game_info = {game_id: [0, 0, 0] for game_id in game_record[0][1:]}

# 遍历所有玩家的游戏记录行(从第二行开始)
for player_data in game_record[1:]:
    # 遍历当前玩家每个游戏的对局结果
    for index, result in enumerate(player_data[1:], start=1):
        current_game = game_record[0][index]
        if result == '1':
            game_info[current_game][0] += 1
            game_info[current_game][1] += 1
        elif result == '0':
            game_info[current_game][0] += 1
            game_info[current_game][2] += 1

输出结果

Game_info = {'G02': [5, 3, 2], 'G05': [5, 3, 2], 'G07': [3, 2, 1], 'G08': [2, 2, 0], 'G10': [1, 1, 0]}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 00:15:03