I need a dictionary structure that has the following properties:
- Double attachment (so, 3-dimensional, so many words)
- Memorizes the order of things added to it for each level.
So, if I add elements to it like this:
# d = something dict-ish d['a']['b']['c'] = 'd' d['a'][1][2] = 3 d['f']['g']['e'] = 'g' d['f'][5][6] = 7 d['a']['foo']['bar'] = 'hello world'
The result of the following understanding:
[(i, j, k, d[i][j][k]) for i in d for j in d[i] for k in d[i][j]]
Will be:
[('a', 'b', 'c', 'd'), ('a', 1, 2, 3), ('a', 'foo', 'bar', 'hello world'), ('f', 'g', 'e', 'g'), ('f', 5, 6, 7)]
I tried using defaultdict to force the use of this structure for new keys, so I do not need to enter it in a long way, for example:
# long way d = OrderedDict() d['a'] = OrderedDict([('b', OrderedDict([('c', 'd')]))]) d['a'][1] = OrderedDict([(2,3)]) # tried to use defaultdict d = defaultdict(lambda: defaultdict(lambda: OrderedDict())) d['a']['b']['c'] = 'd' d['a'][1][2] = 3
But defaultdict does not remember the order of the two upper levels. I'm not sure how to merge the behavior, so obviously the top two levels give the defaultdict behavior, because I declared d as such. How can I achieve the structure I want?