如何遍历二维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
相关产品推荐
相关产品推荐

