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']]
Info for game_record list data:
- * = nothing, just a sign
- GXX = game_id
- PXXX = player_id
- 1 = playing game and win (game finished and recorded)
- 0 = playing game and lose (game finished and recorded)
- -1 = not playing the game (not recorded)
- 503 = in an on-going game (game not finished and not recorded)
for example:
- G02 has been played and finished by 5 players with total of 3 players win and 2 players lose.
- G08 has been played and finished by 2 players with total of 2 players win.
therefore only '1' and '0' matter for the record.
I want to loop this 2D-list to count how many times each game (game_id) has been played, win, and lose by a player. Then store them to dictionary.
Expected Output:
Game_info = {G02: [5, 3, 2], G05: [5, 3, 2], G07: [3, 2, 1], G08: [2, 2, 0], G10: [1, 1, 0]}
CodePudding user response:
Start with a dictionary with all the games in game_record
initialized with [0,0,0]
Game_info = { game:[0,0,0] for game in game_record[0] if game != "*"}
then you can just iterate through game_record
and update values in 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
CodePudding user response:
Try this.
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]}
CodePudding user response:
Here's my solution without library.
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)]
CodePudding user response:
You can save a loop by using NumPy and Boolean arrays:
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]}