如果键的值相同,如何合并两个字典列表?

给定两个字典列表,如何根据特定键的值合并它们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'}},


ITMISS
浏览 164回答 2
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'}]

繁花如伊

我删除了您的一些数据以减少输出 - 您可以使用 2 个字典来快速访问列表中的 id(小队与球员)。如果他们有一个具有相同密钥的小队,您循环一次并更新他们 - 您还可以使用 dicts 断言所有小队是否有球员信息: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 namesquads = [ {'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 - checkkeyed_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 infofor 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 playersfor 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 周内查看它,这可能是一个奖励。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python