简体   繁体   English

通过字典中的键值组合两个字典列表

[英]Combine two lists of dictionaries by value of key in dictionaries

I have two lists whose elements are dictionaries. 我有两个列表,其元素为字典。

list1 = [
    {'id': 1, 'color': 'purple', 'size': 10},
    {'id': 2, 'color': 'red', 'size': 25},
    {'id': 3, 'color': 'orange', 'size': 1},
    {'id': 4, 'color': 'black', 'size': 100},
    {'id': 5, 'color': 'green', 'size': 33}
]

list2 = [
    {'id': 2, 'width': 22, 'age': 22.3},
    {'id': 5, 'width': 9, 'age': 1.7}
]

I want a third list that is the same length as the larger list, and where there is a dictionary element in the smaller list that has an id that matches a dictionary element in the larger list, merge the two dictionaries, so that the final output would look like: 我想要第三个列表的长度与较大列表的长度相同,并且较小列表中的字典元素的ID与较大列表中的字典元素匹配的ID合并两个字典,以便最终输出看起来像:

list3 = [
    {'id': 1, 'color': 'purple', 'size': 10},
    {'id': 2, 'color': 'red', 'size': 25, 'width': 22, 'age': 22.3},
    {'id': 3, 'color': 'orange', 'size': 1},
    {'id': 4, 'color': 'black', 'size': 100},
    {'id': 5, 'color': 'green', 'size': 33, 'width': 9, 'age': 1.7}
]

Ideally if this could be done without looping over both lists, that would be ideal. 理想情况下,如果可以在不循环访问两个列表的情况下完成此操作,那将是理想的。

Try this nested list comprehension with a dict ionary with unpacking, and a next , as well as another list comprehension: 尝试将此嵌套列表理解与带解包的dict一起使用,然后使用next以及另一个列表理解:

list3 = [{**i, **next(iter([x for x in list2 if x['id'] == i['id']]), {})} for i in list1]

And now: 现在:

print(list3)

Is: 方法是:

[{'id': 1, 'color': 'purple', 'size': 10}, {'id': 2, 'color': 'red', 'size': 25, 'width': 22, 'age': 22.3}, {'id': 3, 'color': 'orange', 'size': 1}, {'id': 4, 'color': 'black', 'size': 100}, {'id': 5, 'color': 'green', 'size': 33, 'width': 9, 'age': 1.7}]

Use defaultdict 使用defaultdict

from collections import defaultdict

list1 = [
    {'id': 1, 'color': 'purple', 'size': 10},
    {'id': 2, 'color': 'red', 'size': 25},
    {'id': 3, 'color': 'orange', 'size': 1},
    {'id': 4, 'color': 'black', 'size': 100},
    {'id': 5, 'color': 'green', 'size': 33}
]

list2 = [
    {'id': 2, 'width': 22, 'age': 22.3},
    {'id': 5, 'width': 9, 'age': 1.7}
]

dict1 = defaultdict(dict)

for l in (list1, list2):
    for elem in l:
        dict1[elem['id']].update(elem)
list3 = dict1.values()

print(list(list3))

O/P: O / P:

[
  {
    'id': 1,'color': 'purple','size': 10
  },
  {
    'id': 2,'color': 'red','size': 25,'width': 22,'age': 22.3
  },
  {
    'id': 3,'color': 'orange','size': 1
  },
  {
    'id': 4,'color': 'black','size': 100
  },
  {
    'id': 5, 'color': 'green','size': 33, 'width': 9,'age': 1.7
  }
]

list3 is not guaranteed to be sorted (.values() returns items in no specific order, you can try this to sort. 不保证list3可以排序(.values()不按特定顺序返回项目,您可以尝试按此排序。

from operator import itemgetter
...
new_list = sorted(dict1.values(), key=itemgetter("id"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM