Home > OS >  How to move values inside values in a dictionary to another values inside values in a dictionary?
How to move values inside values in a dictionary to another values inside values in a dictionary?

Time:10-30

  • PXXX = playerid
  • GXX = gameid

Each playerid have multiple gameid, I want to move the played or not played in each gameid in the player_playedgame to the corresponding gameid with the same playerid in the player_statistics.

player_statistics = {'P001': {'G02': ['1', 'S', '5'], 'G05': ['0', 'N', '2'], 'G07': ['0', 'S', '5'], 'G08': ['0', 'N', '3'], 'G10': ['1', 'N', '3']}, 
'P067': {'G02': ['1', 'S', '5'], 'G05': ['1', 'N', '2'], 'G07': ['0', 'S', '5'], 'G08': ['0', 'N', '3'], 'G10': ['0', 'N', '3']}, 
'P218': {'G02': ['0', 'S', '5'], 'G05': ['1', 'N', '2'], 'G07': ['1', 'S', '5'], 'G08': ['0', 'N', '3'], 'G10': ['0', 'N', '3']}, 
'P101': {'G02': ['0', 'S', '5'], 'G05': ['0', 'N', '2'], 'G07': ['1', 'S', '5'], 'G08': ['1', 'N', '3'], 'G10': ['0', 'N', '3']}, 
'P456': {'G02': ['1', 'S', '5'], 'G05': ['1', 'N', '2'], 'G07': ['0', 'S', '5'], 'G08': ['1', 'N', '3'], 'G10': ['0', 'N', '3']}}

player_playedgame = {'P001': {'G02': ['played'], 'G05': ['played'], 'G07': ['notplayed'], 'G08': ['notplayed'], 'G10': ['played']}, 
'P067': {'G02': ['played'], 'G05': ['played'], 'G07': ['played'], 'G08': ['notplayed'], 'G10': ['notplayed']}, 
'P218': {'G02': ['played'], 'G05': ['played'], 'G07': ['played'], 'G08': ['notplayed'], 'G10': ['notplayed']}, 
'P101': {'G02': ['played'], 'G05': ['played'], 'G07': ['played'], 'G08': ['played'], 'G10': ['notplayed']}, 
'P456': {'G02': ['played'], 'G05': ['played'], 'G07': ['notplayed'], 'G08': ['played'], 'G10': ['notplayed']}}

My code:

for keys, values in player_statistics.items():
    for key, value in player_playedgame.items():
        for ke, va in values.items():
            for k, v in value.items():
                if ke == k:
                    player_statistics[keys][ke].append(v)

My code output:

{'P001': {'G02': ['1', 'S', '5', ['played'], ['played'], ['played'], ['played'], ['played']], 'G05': ['0', 'N', '2', ['played'], ['played'], ['played'], ['played'], ['played']], 'G07': ['0', 'S', '5', ['notplayed'], ['played'], ['played'], ['played'], ['notplayed']], 'G08': ['0', 'N', '3', ['notplayed'], ['notplayed'], ['notplayed'], ['played'], ['played']], 'G10': ['1', 'N', '3', ['played'], ['notplayed'], ['notplayed'], ['notplayed'], ['notplayed']]}, 'P067': {'G02': ['1', 'S', '5', ['played'], ['played'], ['played'], ['played'], ['played']], 'G05': ['1', 'N', '2', ['played'], ['played'], ['played'], ['played'], ['played']], 'G07': ['0', 'S', '5', ['notplayed'], ['played'], ['played'], ['played'], ['notplayed']], 'G08': ['0', 'N', '3', ['notplayed'], ['notplayed'], ['notplayed'], ['played'], ['played']], 'G10': ['0', 'N', '3', ['played'], ['notplayed'], ['notplayed'], ['notplayed'], ['notplayed']]}, 'P218': {'G02': ['0', 'S', '5', ['played'], ['played'], ['played'], ['played'], ['played']], 'G05': ['1', 'N', '2', ['played'], ['played'], ['played'], ['played'], ['played']], 'G07': ['1', 'S', '5', ['notplayed'], ['played'], ['played'], ['played'], ['notplayed']], 'G08': ['0', 'N', '3', ['notplayed'], ['notplayed'], ['notplayed'], ['played'], ['played']], 'G10': ['0', 'N', '3', ['played'], ['notplayed'], ['notplayed'], ['notplayed'], ['notplayed']]}, 'P101': {'G02': ['0', 'S', '5', ['played'], ['played'], ['played'], ['played'], ['played']], 'G05': ['0', 'N', '2', ['played'], ['played'], ['played'], ['played'], ['played']], 'G07': ['1', 'S', '5', ['notplayed'], ['played'], ['played'], ['played'], ['notplayed']], 'G08': ['1', 'N', '3', ['notplayed'], ['notplayed'], ['notplayed'], ['played'], ['played']], 'G10': ['0', 'N', '3', ['played'], ['notplayed'], ['notplayed'], ['notplayed'], ['notplayed']]}, 'P456': {'G02': ['1', 'S', '5', ['played'], ['played'], ['played'], ['played'], ['played']], 'G05': ['1', 'N', '2', ['played'], ['played'], ['played'], ['played'], ['played']], 'G07': ['0', 'S', '5', ['notplayed'], ['played'], ['played'], ['played'], ['notplayed']], 'G08': ['1', 'N', '3', ['notplayed'], ['notplayed'], ['notplayed'], ['played'], ['played']], 'G10': ['0', 'N', '3', ['played'], ['notplayed'], ['notplayed'], ['notplayed'], ['notplayed']]}}

Instead of only only and inside the list, the played/notplayed made a new list and spam multiple times.

Expected Output:

player_statistics = {'P001': {'G02': ['1', 'S', '5', 'played'], 'G05': ['0', 'N', '2', 'played'], 'G07': ['0', 'S', '5','notplayed'], 'G08': ['0', 'N', '3', 'notplayed'], 'G10': ['1', 'N', '3','played]}, 
'P067': {'G02': ['1', 'S', '5', 'played'], 'G05': ['1', 'N', '2', 'played'], 'G07': ['0', 'S', '5', 'played'], 'G08': ['0', 'N', '3', 'notplayed'], 'G10': ['0', 'N', '3', 'notplayed']}, 
'P218': {'G02': ['0', 'S', '5', 'played'], 'G05': ['1', 'N', '2', 'played'], 'G07': ['1', 'S', '5', 'played'], 'G08': ['0', 'N', '3', 'notplayed'], 'G10': ['0', 'N', '3', 'notplayed']}, 
'P101': {'G02': ['0', 'S', '5', 'played'], 'G05': ['0', 'N', '2', 'played'], 'G07': ['1', 'S', '5', 'played'], 'G08': ['1', 'N', '3', 'played'], 'G10': ['0', 'N', '3', 'notplayed']}, 
'P456': {'G02': ['1', 'S', '5', 'played'], 'G05': ['1', 'N', '2', 'played'], 'G07': ['0', 'S', '5', 'notplayed'], 'G08': ['1', 'N', '3', 'played'], 'G10': ['0', 'N', '3', 'notplayed'}

What did I do wrong on my code? I would appreciate it if you can point out my mistake and show me the appropriate way to achieve the result. Thanks in advance.

CodePudding user response:

Try that way:

for player_id, player_games in player_statistics.items():
    for game_id, game_values in player_games.items():
        try:
            value = player_playedgame[player_id][game_id][0]
            player_statistics[player_id][game_id].append(value)
        except IndexError:
            continue

It will always take the first value of the list player_playedgame[player][game] as I noticed that the list hold always only one element. It does also not iterate over the second dict player_playedgame and directly tries to access the values.

CodePudding user response:

Use zip to read the dict simultaneously.

player_statistics = {'P001': {'G02': ['1', 'S', '5'], 'G05': ['0', 'N', '2'], 'G07': ['0', 'S', '5'], 'G08': ['0', 'N', '3'], 'G10': ['1', 'N', '3']}, 
'P067': {'G02': ['1', 'S', '5'], 'G05': ['1', 'N', '2'], 'G07': ['0', 'S', '5'], 'G08': ['0', 'N', '3'], 'G10': ['0', 'N', '3']}, 
'P218': {'G02': ['0', 'S', '5'], 'G05': ['1', 'N', '2'], 'G07': ['1', 'S', '5'], 'G08': ['0', 'N', '3'], 'G10': ['0', 'N', '3']}, 
'P101': {'G02': ['0', 'S', '5'], 'G05': ['0', 'N', '2'], 'G07': ['1', 'S', '5'], 'G08': ['1', 'N', '3'], 'G10': ['0', 'N', '3']}, 
'P456': {'G02': ['1', 'S', '5'], 'G05': ['1', 'N', '2'], 'G07': ['0', 'S', '5'], 'G08': ['1', 'N', '3'], 'G10': ['0', 'N', '3']}}

player_playedgame = {'P001': {'G02': ['played'], 'G05': ['played'], 'G07': ['notplayed'], 'G08': ['notplayed'], 'G10': ['played']}, 
'P067': {'G02': ['played'], 'G05': ['played'], 'G07': ['played'], 'G08': ['notplayed'], 'G10': ['notplayed']}, 
'P218': {'G02': ['played'], 'G05': ['played'], 'G07': ['played'], 'G08': ['notplayed'], 'G10': ['notplayed']}, 
'P101': {'G02': ['played'], 'G05': ['played'], 'G07': ['played'], 'G08': ['played'], 'G10': ['notplayed']}, 
'P456': {'G02': ['played'], 'G05': ['played'], 'G07': ['notplayed'], 'G08': ['played'], 'G10': ['notplayed']}}


data = {}

for (k1, v1), (k2, v2) in zip(player_statistics.items(), player_playedgame.items()):
    d = {}
    for (k3, v3), (k4, v4) in zip(v1.items(), v2.items()):
        v3.append(v4[0])  # append data from player_playedgame indexed at 0
        d.update({k3: v3})
    data.update({k1: d})

print(data)
# {'P001': {'G02': ['1', 'S', '5', 'played'], 'G05': ['0', 'N', '2', 'played'], 'G07': ['0', 'S', '5', 'notplayed'], 'G08': ['0', 'N', '3', 'notplayed'], 'G10': ['1', 'N', '3', 'played']}, 'P067': ...

  • Related