Merging arbitrarily nested dictionaries, maintaini

2019-09-18 17:24发布

问题:

This is my input:

dict1 = { 'home': 'Documents'}
dict2 = { 'home': {'Documents': 'projects'}}
dict3 = {'projects': 'test.py'}
dict4 = {'projects': 'test2.py'}
dict5 = {'Documents': 'old_memes'}
dict6 = {'old_memes': 'me_gusta.jpg'}
dict7 = {'old_memes': 'fml.jpg'}

My goal is to have an output of a single dictionary, like this:

final_dict = {'home': 
                      {'Documents':
                                   [
                                    {'projects': ['test.py','test2.py']},
                                    {'old_memes':['me_gusta.jpg','fml.jpg']}
                                   ]
                       }
              }

I searched around, but couldn't find nothing that do this. I tried using defaultdict but it didn't work either. If this is not possible with dictionaties, can you point me another data structure that can do this(and be converted to a JSON)?