Ich hoffe, ich erfinde das Rad nicht neu, aber die Lösung ist ziemlich kurz. Und super zu codieren.
def merge(source, destination):
"""
run me with nosetests --with-doctest file.py
>>> a = { 'first' : { 'all_rows' : { 'pass' : 'dog', 'number' : '1' } } }
>>> b = { 'first' : { 'all_rows' : { 'fail' : 'cat', 'number' : '5' } } }
>>> merge(b, a) == { 'first' : { 'all_rows' : { 'pass' : 'dog', 'fail' : 'cat', 'number' : '5' } } }
True
"""
for key, value in source.items():
if isinstance(value, dict):
# get node or create one
node = destination.setdefault(key, {})
merge(value, node)
else:
destination[key] = value
return destination
So ist die Idee, die Quelle zum Ziel zu kopieren, und jedes Mal ist es ein dict in der Quelle, recurse. Sie werden also einen Fehler haben, wenn in A ein bestimmtes Element ein Diktat und in B ein anderer Typ enthält.
[EDIT] wie in den Kommentaren, sagte die Lösung bereits hier war: https://stackoverflow.com/a/7205107/34871
Dup http://stackoverflow.com/questions/7204805/python-dictionaries-of-dictionaries-merge – GLES