Hi. I am trying to create a new dictionary by using an existing list of players for its keys and values from an existing dictionary for its values. The values need to be summed up by player and position as explained further below. The issue is firstly that I don’t know why the very below code doesn’t produce the expected result and secondly how to get this kind of an approach working if possible. (I have a code further below that produces the expected result but it’s not optimal in the big picture.)
If I try to create the new dictionary and lists for its values with a loop as in the below code:
players = ['a', 'b', 'c']
stats_combinations = {('a', 'b', 'c'): 0.25, ('b', 'a', 'c'): 0.3, ('c', 'a', 'b'): 0.1, ('b', 'c', 'a'): 0.15, ('c', 'b', 'a'): 0.15, ('a', 'c', 'b'): 0.05}
support = []
stats_players = {}
run = 0
for plr in players:
support.append(0)
stats_players[plr] = support
for key in stats_combinations:
for unit in key:
stats_players[unit][run] += stats_combinations[key]
run += 1
run = 0
for key, value in stats_players.items():
print(f"key: {key}: {value}")
I get a different and not the expected result (1.0 for all values). However, if I run the below code where I have defined the dictionary ‘stats_players’ with keys (players) and values (lists of values) differently, I get the expected result:
players = ['a', 'b', 'c']
stats_combinations = {('a', 'b', 'c'): 0.25, ('b', 'a', 'c'): 0.3, ('c', 'a', 'b'): 0.1, ('b', 'c', 'a'): 0.15, ('c', 'b', 'a'): 0.15, ('a', 'c', 'b'): 0.05}
stats_players = {'a': [0,0,0], 'b': [0,0,0], 'c': [0,0,0]}
run = 0
for key in stats_combinations:
for unit in key:
stats_players[unit][run] += stats_combinations[key]
run += 1
run = 0
for key, value in stats_players.items():
print(f"key: {key}: {value}")
For further clarification, each player (key) in the new dictionary 'stats_players’ has a list as its value. The values in those lists represent the sum of player combination values from the existing dictionary ‘stats_combinations’ based on the position: First value in the list for each player is the sum of values where that player is first in the player combinations from the ‘stats_combinations’, for example for player a:
the new dictionary should result as stats_players = {‘a’: [0.25+0.05=0.3, 0.3+0.1=0.4, 0.15+0.15=0.3], ‘b’: …}.
And for all the three players the new dictionary should produce the below key-value combinations (don’t mind the non-rounding):
key: a: [0.3, 0.4, 0.3]
key: b: [0.44999999999999996, 0.4, 0.15000000000000002]
key: c: [0.25, 0.2, 0.55].
Hope this isn’t the worst ever try for the first call for help in this forum.
Thanks in advance
-T