为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

ITMISS 2022-10-25 10:38:38
给定两个字典列表,如何根据特定键的值合并它们id?#List of dicts1players = [ {'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 dicts2squad = [ {'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'}},
查看完整描述

2 回答

?
素胚勾勒不出你

TA贡献1827条经验 获得超9个赞

尝试这个:

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'}]


查看完整回答
反对 回复 2022-10-25
?
繁花如伊

TA贡献2012条经验 获得超12个赞

我删除了您的一些数据以减少输出 - 您可以使用 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 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 周内查看它,这可能是一个奖励。


查看完整回答
反对 回复 2022-10-25
  • 2 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信