【问题标题】:How to loop and count how many times each game has been played?如何循环并计算每场比赛已经玩了多少次?
【发布时间】:2021-12-13 05:16:33
【问题描述】:
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']]

game_record 列表数据的信息:

  • * = 没有,只​​是一个符号
  • GXX = game_id
  • PXXX = player_id
  • 1 = 玩游戏并获胜(游戏结束并记录)
  • 0 = 玩游戏但输了(游戏结束并记录)
  • -1 = 不玩游戏(未录制)
  • 503 = 在正在进行的游戏中(游戏未完成且未录制)

例如:

  • G02 已由 5 名玩家完成,共 3 名获胜和 2 名失败。
  • G08 已由 2 名玩家完成并完成,共有 2 名玩家获胜。

因此只有“1”和“0”才有意义。

我想循环这个 2D 列表来计算每个游戏 (game_id) 被玩家玩了多少次、赢了和输了多少次。然后将它们存储到字典中。

预期输出:

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

【问题讨论】:

    标签: python python-3.x loops dictionary


    【解决方案1】:

    从字典开始,game_record 中的所有游戏都用[0,0,0] 初始化

    Game_info = { game:[0,0,0] for game in game_record[0] if game != "*"}
    

    然后您可以遍历game_record 并更新Game_info 中的值

    n, m = len(game_record[0]), len(game_record)
    
    for y in range(1,n):
         g_id = game_record[0][y]
         for x in range(1,m):
             res = game_record[x][y]
             if res == '1':
                 Game_info[g_id][0] += 1
                 Game_info[g_id][1] += 1
             elif res == '0':
                 Game_info[g_id][0] += 1
                 Game_info[g_id][2] += 1         
    

    【讨论】:

      【解决方案2】:

      您可以使用NumPy 和布尔数组来保存循环:

      import numpy as np
      
      results = np.array(game_record)[1:, 1:].astype(int)
      
      won_by_game = (results == 1).sum(axis=0)
      lost_by_game = (results == 0).sum(axis=0)
      played_by_game = won_by_game + lost_by_game
      
      game_info = {game: [played, won, lost] 
                   for game, played, won, lost in 
                   zip(game_record[0][1:], played_by_game, 
                       won_by_game, lost_by_game)}
      game_info
      
      {'G02': [5, 3, 2],
       'G05': [5, 3, 2],
       'G07': [3, 2, 1],
       'G08': [2, 2, 0],
       'G10': [1, 1, 0]}
      

      【讨论】:

        【解决方案3】:

        试试这个。

        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']
        ]
        
        
        res = {}
        
        for i in range(1, 6):
            w, l = 0, 0
            label = game_record[0][i]
            for j in range(1, 6):   
                v = game_record[j][i]  # access matrix value (column-wise) except row 0 and col 0
                if v == '-1' or v == '503':
                    continue
                if v == '1':
                    w += 1
                else:
                    l += 1
            g = w + l
            res.update({label: [g, w, l]})
        
        print(res)
        # {'G02': [5, 3, 2], 'G05': [5, 3, 2], 'G07': [3, 2, 1], 'G08': [2, 2, 0], 'G10': [1, 1, 0]}
        

        【讨论】:

          【解决方案4】:

          这是我没有库的解决方案。

          names = game_record[0][1:]  
          game_record = [*zip(*game_record[1:])][1:]  # Transpose and drop
          game_record = [list(map(int, x)) for x in game_record]  # Turn into int
          solution = dict()
          for i, game in enumerate(game_record):
              game = [x for x in game if x not in [-1, 503]] # Filter unwanted
              solution[names[i]] = [len(game), sum(game), len(game) - sum(game)]
          

          【讨论】:

            猜你喜欢
            • 2021-07-18
            • 1970-01-01
            • 1970-01-01
            • 2019-01-18
            • 2014-04-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多