[英]how to merge two nested list of dict into new one
I have two nested list of dicts, like to merge with the same grouped key.我有两个嵌套的字典列表,喜欢与相同的分组键合并。 For example:
例如:
DIC1: DIC1:
{'SW-01': [{'Aip': '192.168.2.1',
'Bip': '192.168.2.0',
{'Aip': '192.168.3.3',
'Bip': '192.168.3.2'}]}
DIC2: DIC2:
{'SW-01': [{'Aip': '192.168.1.19',
'Bip': '192.168.1.18'}],
'SW-02': [{'Aip': '192.168.1.83',
'Aip': '192.168.1.82',
{'Aip': '192.168.1.85',
'Bip': '192.168.1.84'}]}
How to merge two dict above, with group key?如何用组键合并上面的两个字典? Here is the code I have used tried to merge, but I get SW1 data twice from the DIC1 and DIC2.
这是我尝试合并的代码,但我从 DIC1 和 DIC2 获得了两次 SW1 数据。
d = {}
for key in (*dic1, *dic2):
try:
d.setdefault(key,[]).append(dic1[key])
except KeyError:
pass
try:
d.setdefault(key,[]).append(dic2[key])
except KeyError:
pass
pprint(d)
Try to list()
your dictionaries and then append two lists.尝试
list()
您的字典,然后是 append 两个列表。 ().append
does not support list operators ().append
不支持列表运算符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.