I have this multidimensional dict:
a = {'a' : 'b', 'c' : {'d' : 'e'}}
And a simple function is written to smooth this dict:
def __flatten(self, dictionary, level = []):
tmp_dict = {}
for key, val in dictionary.items():
if type(val) == dict:
tmp_dict.update(self.__flatten(val, level + [key]))
else:
tmp_dict['.'.join(level + [key])] = val
return tmp_dict
After calling this function with dict, aI get the result:
{'a' : 'b', 'c.d' : 'e'}
Now, with a few instructions on this flattened dict, I need to build a new, multidimensional dict from this flattened dict. Example:
>> unflatten({'a' : 0, 'c.d' : 1))
{'a' : 0, 'c' : {'d' : 1}}
The only problem I am facing is that I do not have a function unflatten:)
Can someone help with this? I do not know how to do that.
EDIT:
Another example:
{'a' : 'b', 'c.d.e.f.g.h.i.j.k.l.m.n.o.p.r.s.t.u.w' : 'z'}
Must be after unflatten:
{'a': 'b', 'c': {'d': {'e': {'f': {'g': {'h': {'i': {'j': {'k': {'l': {'m': {'n': {'o': {'p': {'r': {'s': {'t': {'u': {'w': 'z'}}}}}}}}}}}}}}}}}}}
And further:
{'a' : 'b', 'c.d' : 'z', 'c.e' : 1}
To:
{'a' : 'b', 'c' : {'d' : 'z', 'e' : 1}}
This greatly increases the complexity of the task, I know. That is why I had a problem with this and did not find any resolution in the clock.