给出两个字典列表,如何根据特定键id
的值将它们合并?
#List of dicts1
players = [ {'age': '19 years 275 days',
'birth': '24 July 2000',
'birth_exact': 964396800000.0,
'country': 'Ecuador',
'first': 'Leonardo',
'id': 74562.0,
'isoCode': 'EC',
'last': 'Campana',
'loan': False,
'name': 'Leonardo Campana',
'nationalTeam': 'Ecuador',
'playerId': 179304.0,
'position': 'F',
'positionInfo': 'Centre Striker',
'season': 2019,
'shirtNum': None},
{'age': '24 years 186 days',
'birth': '21 October 1995',
'birth_exact': 814233600000.0,
'country': 'Portugal',
'first': 'Daniel',
'id': 11362.0,
'isoCode': 'PT',
'last': 'Castelo Podence',
'loan': False,
'name': 'Daniel Podence',
'nationalTeam': 'Portugal',
'playerId': 180050.0,
'position': 'M',
'positionInfo': 'Left/Right Winger',
'season': 2019,
'shirtNum': 10.0}]
#List of dicts2
squad = [ {'id': 11362.0,
'team': 'Arsenal',
'team_id':1,
'team_shortName': 'Arsenal'},
{'id': 74562.0,
'team': 'Wolverhampton Wanderers',
'team_id': 38,
'team_shortName': 'Wolves'}]
我想将squad
中的信息合并到players
中。
我尝试了以下操作:
p_sort = sorted(players, key=lambda k: k['id'])
s_sort = sorted(squad, key=lambda k: k['id'])
l3 = [{**u, **v} for u, v in zip_longest(p_sort, s_sort, fillvalue={})]
return l3
但是一个小问题是这两个字典的长度不同,因此该解决方案无法按预期工作。该如何解决?
预期输出:
l3 = [ {'age': '19 years 275 days',
'birth': '24 July 2000',
'birth_exact': 964396800000.0,
'country': 'Ecuador',
'first': 'Leonardo',
'id': 74562.0,
'isoCode': 'EC',
'last': 'Campana',
'loan': False,
'name': 'Leonardo Campana',
'nationalTeam': 'Ecuador',
'playerId': 179304.0,
'position': 'F',
'positionInfo': 'Centre Striker',
'season': 2019,
'shirtNum': None,
'team': 'Wolverhampton Wanderers',
'team_id': 38,
'team_shortName': 'Wolves'}},
{'age': '24 years 186 days',
'birth': '21 October 1995',
'birth_exact': 814233600000.0,
'country': 'Portugal',
'first': 'Daniel',
'id': 11362.0,
'isoCode': 'PT',
'last': 'Castelo Podence',
'loan': False,
'name': 'Daniel Podence',
'nationalTeam': 'Portugal',
'playerId': 180050.0,
'position': 'M',
'positionInfo': 'Left/Right Winger',
'season': 2019,
'shirtNum': 10.0},
'team': 'Arsenal',
'team_id': 1,
'team_shortName': 'Arsenal'}]
答案 0 :(得分:2)
尝试一下:
res = [{**x, **y} for y in players for x in squad if x['id'] == y['id']]
d = [dict(sorted(d.items())) for d in res]
输出:
[{'age': '19 years 275 days', 'birth': '24 July 2000', 'birth_exact': 964396800000.0, 'country': 'Ecuador', 'first': 'Leonardo', 'id': 74562.0, 'isoCode': 'EC', 'last': 'Campana', 'loan': False, 'name': 'Leonardo Campana', 'nationalTeam': 'Ecuador', 'playerId': 179304.0, 'position': 'F', 'positionInfo': 'Centre Striker', 'season': 2019, 'shirtNum': None, 'team': 'Wolverhampton Wanderers', 'team_id': 38, 'team_shortName': 'Wolves'}, {'age': '24 years 186 days', 'birth': '21 October 1995', 'birth_exact': 814233600000.0, 'country': 'Portugal', 'first': 'Daniel', 'id': 11362.0, 'isoCode': 'PT', 'last': 'Castelo Podence', 'loan': False, 'name': 'Daniel Podence', 'nationalTeam': 'Portugal', 'playerId': 180050.0, 'position': 'M', 'positionInfo': 'Left/Right Winger', 'season': 2019, 'shirtNum': 10.0, 'team': 'Arsenal', 'team_id': 1, 'team_shortName': 'Arsenal'}]
答案 1 :(得分:1)
我删除了一些数据以减少输出-您可以使用2个字典来快速访问列表中的ID(小队或球员)。
您可以在玩家列表上循环一次,如果他们的小队使用了相同的按键,则可以更新它们-您还可以使用dict断言所有小队是否都获得了玩家信息:
players = [ {'age': '19 years 275 days',
'birth': '24 July 2000',
'id': 74562.0 },
{'age': '24 years 186 days',
'birth': '21 October 1995',
'birth_exact': 814233600000.0,
'id': 11362.0,},
{'age': '24 years 186 days',
'birth': '21 October 1995',
'birth_exact': 814233600000.0,
'id':42,}]
# fixed name
squads = [ {'id': 11362.0,
'team': 'Arsenal',
'team_id':1,
'team_shortName': 'Arsenal'},
{'id': 74562.0,
'team': 'Wolverhampton Wanderers',
'team_id': 38,
'team_shortName': 'Wolves'},
{'id': -3,
'team': 'Wolverhampton Wanderers',
'team_id': 38,
'team_shortName': 'Wolves'}]
p_sort = sorted(players, key=lambda k: k['id'])
s_sort = sorted(squads, key=lambda k: k['id'])
keyed_players = {a["id"]:a for a in p_sort} # easier access for IN - check
keyed_squads = {a["id"]:a for a in s_sort} # easier access for IN - check
# iterate players and modify them if squad info given, else printout info
for player in p_sort:
if player["id"] in keyed_squads:
player.update(keyed_squads[player["id"]])
else:
print("player", player["id"], "has no squad info")
print(p_sort)
# get squads without players
for s in keyed_squads:
if s not in keyed_players:
print("squad", s, "has no player info")
输出:
player 42 has no squad info
[{'age': '24 years 186 days', 'birth': '21 October 1995',
'birth_exact': 814233600000.0, 'id': 42},
{'age': '24 years 186 days', 'birth': '21 October 1995',
'birth_exact': 814233600000.0, 'id': 11362.0,
'team': 'Arsenal', 'team_id': 1, 'team_shortName': 'Arsenal'},
{'age': '19 years 275 days', 'birth': '24 July 2000', 'id': 74562.0,
'team': 'Wolverhampton Wanderers', 'team_id': 38, 'team_shortName': 'Wolves'}]
squad -3 has no player info
确定的代码肯定比您的行多-但是要清楚什么是什么-如果您在4周内查看它,那将是一个好处。