Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 合并具有相同键值的单个词典列表_Python - Fatal编程技术网

Python 合并具有相同键值的单个词典列表

Python 合并具有相同键值的单个词典列表,python,Python,我可以在这里看到许多类似的问题,但我找不到一个可以帮助我获得所需输出的问题 我有一个具有相同ID但具有不同键值对的字典列表,我想将所有这些键值对合并到一个列表条目中,下面是数据示例和所需的输出 谢谢你的帮助 data = [ {'id': '10', 'animal' : 'cat'}, {'id': '11', 'animal' : 'dog'}, {'id': '3', 'animal' : 'pigeon'}, {'id': '10', 'color' :

我可以在这里看到许多类似的问题,但我找不到一个可以帮助我获得所需输出的问题

我有一个具有相同ID但具有不同键值对的字典列表,我想将所有这些键值对合并到一个列表条目中,下面是数据示例和所需的输出

谢谢你的帮助

data = [
    {'id': '10', 'animal' : 'cat'},
    {'id': '11', 'animal' : 'dog'},
    {'id': '3', 'animal' : 'pigeon'},
    {'id': '10', 'color' : 'yellow'},
    {'id': '11', 'color' : 'brown'},
    {'id': '3', 'color' : 'grey'},
    {'id': '10', 'type' : 'furry'},
    {'id': '11', 'type' : 'fluffy'},
    {'id': '3', 'type' : 'dirty'},
]
期望输出

data = [
    {'id': '10', 'animal' : 'cat', 'color' : 'yellow', 'type' : 'furry'},
    {'id': '11', 'animal' : 'dog', 'color' : 'brown', 'type' : 'fluffy'},
    {'id': '3', 'animal' : 'pigeon', 'color' : 'grey', 'type' : 'dirty'},
]

实现这一点有多种方法,其中之一是
defaultdict

[1]中的
:数据=[
…:{'id':'10','animal':'cat'},
…:{'id':'11','animal':'dog'},
…:{'id':'3','动物':'鸽子'},
…:{'id':'10','color':'yellow'},
…:{'id':'11','color':'brown'},
…:{'id':'3','color':'grey'},
…:{'id':'10','type':'furry'},
…:{'id':'11','type':'fluffy'},
…:{'id':'3','type':'dirty'},
...: ]
在[2]中:从集合导入defaultdict
…:ids=defaultdict(dict)
…:对于数据中的d:
…:id[d[“id”]]。更新(d)
...:
在[6]中:列表(ids.values())
出[6]:
[{'id':'10','animal':'cat','color':'yellow','type':'furry'},
{'id':'11','animal':'dog','color':'brown','type':'fluffy'},
{'id':'3','animal':'鸽子','color':'grey','type':'dirty'}]
在Python 3.9(ETA 2020年秋季)中,您可以使用
|
操作符将
dict
与相同的
id
键合并

from itertools import groupby
from operator import or_, itemgetter
from functools import reduce

# I know *why* groupby doesn't have an option to
# sort your data first, but that doesn't mean I can't
# wish that it could...
def group(data, key):
    "Iterate over groups of dicts considered equal according to key"
    yield from map(itemgetter(1), groupby(sorted(data, key=key), key))

data = [
    {'id': '10', 'animal' : 'cat'},
    {'id': '11', 'animal' : 'dog'},
    {'id': '3', 'animal' : 'pigeon'},
    {'id': '10', 'color' : 'yellow'},
    {'id': '11', 'color' : 'brown'},
    {'id': '3', 'color' : 'grey'},
    {'id': '10', 'type' : 'furry'},
    {'id': '11', 'type' : 'fluffy'},
    {'id': '3', 'type' : 'dirty'},
    ]


# E.g., {'id': 10, 'animal': 'cat'} | {'id': 10, 'color': 'yellow'}
#  == {'id': 10, 'animal': 'cat', 'color': 'yellow'}
data = [reduce(or_, ds) for ds in group(data, itemgetter('id'))]

您可以使用
groupby
ChainMap

from itertools import groupby
from collections import ChainMap

id_getter = lambda x: x['id']
gp = groupby(sorted(data, key=id_getter), key=id_getter)
result = [dict(ChainMap(*a)) for _, a in gp]
groupby
处理已排序的集合,因此在调用
groupby


ChainMap
用于将词典列表合并到单个词典

我想我无法记住
ChainMap
的存在。当有人发布使用它的答案时,我总是(高兴地)感到惊讶。