python - Dictionaries of dictionaries merge -


i need merge multiple dictionaries, here's have instance:

dict1 = {1:{"a":{a}},2:{"b":{b}}}  dict2 = {2:{"c":{c}}, 3:{"d":{d}} 

with a b c , d being leaves of tree, {"info1":"value", "info2":"value2"}

there unknown level(depth) of dictionaries, {2:{"c":{"z":{"y":{c}}}}}

in case represents directory/files structure nodes being docs , leaves being files.

i want merge them obtain dict3={1:{"a":{a}},2:{"b":{b},"c":{c}},3:{"d":{d}}}

i'm not sure how python.

this quite tricky - particularly if want useful error message when things inconsistent, while correctly accepting duplicate consistent entries (something no other answer here does....)

assuming don't have huge numbers of entries recursive function easiest:

def merge(a, b, path=none):     "merges b a"     if path none: path = []     key in b:         if key in a:             if isinstance(a[key], dict) , isinstance(b[key], dict):                 merge(a[key], b[key], path + [str(key)])             elif a[key] == b[key]:                 pass # same leaf value             else:                 raise exception('conflict @ %s' % '.'.join(path + [str(key)]))         else:             a[key] = b[key]     return  # works print(merge({1:{"a":"a"},2:{"b":"b"}}, {2:{"c":"c"},3:{"d":"d"}})) # has conflict merge({1:{"a":"a"},2:{"b":"b"}}, {1:{"a":"a"},2:{"b":"c"}}) 

note mutates a - contents of b added a (which returned). if want keep a call merge(dict(a), b).

agf pointed out (below) may have more 2 dicts, in case can use:

reduce(merge, [dict1, dict2, dict3...]) 

where added dict1.

[note - edited initial answer mutate first argument; makes "reduce" easier explain]

ps in python 3, need from functools import reduce


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -