【发布时间】:2021-11-26 07:44:32
【问题描述】:
我有两个字典,如下所示。两个字典都有一个字典列表作为与其properties 键关联的值;这些列表中的每个字典都有一个id 键。我希望将我的两个字典合并为一个,这样结果字典中的properties 列表对于每个id 只有一个字典。
{
"name":"harry",
"properties":[
{
"id":"N3",
"status":"OPEN",
"type":"energetic"
},
{
"id":"N5",
"status":"OPEN",
"type":"hot"
}
]
}
和其他列表:
{
"name":"harry",
"properties":[
{
"id":"N3",
"type":"energetic",
"language": "english"
},
{
"id":"N6",
"status":"OPEN",
"type":"cool"
}
]
}
我想要达到的输出是:
"name":"harry",
"properties":[
{
"id":"N3",
"status":"OPEN",
"type":"energetic",
"language": "english"
},
{
"id":"N5",
"status":"OPEN",
"type":"hot"
},
{
"id":"N6",
"status":"OPEN",
"type":"cool"
}
]
}
由于id: N3 在两个列表中都很常见,所以这两个字典应该与所有字段合并。到目前为止,我已经尝试使用 itertools 和
ds = [d1, d2]
d = {}
for k in d1.keys():
d[k] = tuple(d[k] for d in ds)
有人可以帮忙解决这个问题吗?
【问题讨论】:
-
@AlexWaygood 我的错。我很抱歉。它的两个字典
标签: python python-3.x dictionary