- PXXX = playerid
- GXX = gameid
每个playerid都有多个gameid,我想将player_playedgame中的每个gameid中的玩过或未玩过移动到player_statistics中具有相同playerid的相应gameid
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']}}
我代码:
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)
我的代码输出:
{'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']]}}
播放/未播放列表不再是only和within,而是创建了一个新列表并多次发送垃圾邮件。
预期输出:
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'}
我的代码哪里做错了?如果您能指出我的错误,并告诉我达到这个结果的适当方法,我将不胜感激。提前谢谢。
试试:
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
它总是取列表player_playedgame[player][game]的第一个值,因为我注意到列表总是只包含一个元素。它也不会遍历第二个字典player_playedgame,而是直接尝试访问这些值。
使用zip同时读取字典
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': ...