1

I am after a little advice, please.

I am trying to combine multiple dictionary like objects - branches - into a single dictionary like object - a tree. For instance:

d1 = {'alpha':{'one':{'a':1}}}
d2 = {'alpha':{'one':{'b':2}}}
d3 = {'alpha':{'two':{'a':3}}}
d4 = {'alpha':{'two':{'b':{'rock':4}}}}
d5 = {'bravo':{'one':{'a':{'paper':5}}}}

Should become:

d_tot = {
    'alpha':{
        'one':{
            'a':1,
            'b':2,
            },
        'two':{
            'a':3,
            'b':{
                'rock':4
            }
        }
        },
    'bravo':{
        'one':{
            'a':{
                'paper':5
            }
        }
    }
}

There will be hundreds of branches of varying length to combine into a tree.

As mentioned previously, these are "dictionary like objects" or bastardised dictionaries that look like the below:

class bastardised_dict(dict):
    def __init__(self, *args):
        super(dict, self).__init__(*args)
        self.__dict__ = self

It is important that they stay as bastardised dictionaries and that the final output is also a bastard. It allows access like: d_tot.alpha.one.a

Please say if I have missed anything material out. Apologies if I am wrecking the way dictionaries/classes are meant to be used - I am too far down the rabbit hole to turn back.

Thanks in advance for giving it some thought (even if you don't have the solution, I appreciate your time).

CDJB
  • 14,043
  • 5
  • 29
  • 55

0 Answers0