我想组合两个嵌套词典
d1 = {"admin": {"key1": "v2"}}
d2 = {"admin": {"key2": "v3"},
"user": {"something": "else"}}
这应该结合到:
d = {"admin": {"key1": "v2",
"key2": "v3"},
"user": {"something": "else"}}
除了迭代第一个键之外,还有一种简单的方法吗?
答案 0 :(得分:4)
您可以使用defaultdict
将第一级的每个值都设为dict
。从dict中使用update()
之后。
>>> from collections import defaultdict
>>> d = defaultdict(dict)
>>> d1 = {"admin": {"key1": "v2"}}
>>> d2 = {"admin": {"key2": "v3"},
... "user": {"something": "else"}}
>>> for dd in [d1,d2]:
... for k,v in dd.items():
... d[k].update(v)
...
>>> d
defaultdict(<type 'dict'>, {'admin': {'key2': 'v3', 'key1': 'v2'}, 'user': {'something': 'else'}})
或者以更紧凑的形式,您可以使用map()
...我真的不喜欢它但是很有机会
>>> for dd in [d1,d2]:
... map(lambda x:d[x[0]].update(x[1]),dd.items())
答案 1 :(得分:1)
如果只有一个嵌套级别:
>>> d1 = {"admin": {"key1": "v2"}}
>>> d2 = {"admin": {"key2": "v3"},
... "user": {"something": "else"}}
>>> keys = list(d1) + list(d2)
>>> d = {k: dict(d1.get(k, {}).items() + d2.get(k, {}).items()) for k in keys}
>>> d
{'admin': {'key1': 'v2', 'key2': 'v3'}, 'user': {'something': 'else'}}
更深的嵌套需要递归。
答案 2 :(得分:1)
from itertools import chain
from collections import defaultdict
new_d = defaultdict(dict)
for k,v in chain(d1.iteritems(),d2.iteritems()):
new_d[k].update(v)
print(new_d)
defaultdict(<type 'dict'>, {'admin': {'key2': 'v3', 'key1': 'v2'}, 'user': {'something': 'else'}})
答案 3 :(得分:1)
from copy import deepcopy
d1 = {"admin": {"key1": "v2"}}
d2 = {"admin": {"key2": "v3"},
"user": {"something": "else"}}
def merge(one, two):
if isinstance(two, dict):
result = deepcopy(one)
for key, value in two.iteritems():
if key in result and isinstance(result[key], dict):
result[key] = merge(result[key], value)
else:
result[key] = deepcopy(value)
return result
return two
print merge(d1, d2)
输出
{'admin': {'key2': 'v3', 'key1': 'v2'}, 'user': {'something': 'else'}}