, dict
, dict.update()
. , , , dict(a, **b)
a
b
b
, , a
"foo"
b
"foo"
import json
for k in a.keys():
try:
a[k].update(b[k])
except KeyError:
continue
print(json.dumps(a, indent=4))
, "1" ,"2" ,"3"
dict
b
"4"
dict
a
. dict n , . , :
:
a = {
"foo" : {
"1" : {
"foo" : {"1": True,
"2": False},
},
"2" : {
"foo" : True,
},
"3" : {
"foo" : True,
},
"4" : {
"foo" : True
}
},
}
b = {
"foo" : {
"1" : {
"foo" : {"1": True,
"3": True},
},
"2" : {
"foo" : True,
},
"3" : {
"foo" : False,
}
}
}
:
import json
def update(dctA, dctB):
if isinstance(dctA, dict) and isinstance(dctB, dict):
for k in set(dctA.keys()) & set(dctB.keys()):
update(dctA[k], dctB[k])
try:
dctA[k].update(dctB[k])
dctB.pop(k)
except (KeyError, AttributeError):
continue
return dctA
print(json.dumps(update(a, b), indent=4))
:
{
"foo": {
"1": {
"foo": {
"1": true,
"2": false,
"3": true
}
},
"2": {
"foo": true
},
"3": {
"foo": false
},
"4": {
"foo": true
}
}
}
, "3"
"foo"
false
, "4"
dict
b
"4"
. , , "foo"
, {"1": True, "2": False}
{"1": True, "3": True}
{"1": true, "2": false, "3": true}
. "1"
, "2"
, dict
b
"2"
, "3"
, dict
b
.