Does anyone know if there's a standard class for an infinitely nestable dictionary in Python?
I'm finding myself repeating this pattern:
d = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))
d['abc']['def']['xyz'] += 1
If I want to add "another layer" (e.g. d['abc']['def']['xyz']['wrt']
), I have to define another nesting of defaultdicts.
To generalize this pattern, I've written a simple class that overrides __getitem__
to automatically create the next nested dictionary.
e.g.
d = InfiniteDict(('count',0),('total',0))
d['abc']['def']['xyz'].count += 0.24
d['abc']['def']['xyz'].total += 1
d['abc']['def']['xyz']['wrt'].count += 0.143
d['abc']['def']['xyz']['wrt'].total += 1
However, does anyone know of a pre-existing implementation of this idea? I've tried Googling, but I'm not sure what this would be called.
Now that we understand nested dictionaries and defaultdicts , we can get into nested defaultdicts . This is concept is extremely powerful as it allows you to build complex dictionaries with a simple initialization. The only caveat is that you need to know the depth of your data structure in advance.
The default value there is not 1, it's 0.
@Jonathan: Yes sure, the argument of a defaultdict (in this case is lambda : defaultdict(int) ) will be called when you try to access a key that don't exist and the return value of it will be set as the new value of this key which mean in our case the value of d[Key_dont_exist] will be defaultdict(int) , and if you try ...
The Python defaultdict type behaves almost exactly like a regular Python dictionary, but if you try to access or modify a missing key, then defaultdict will automatically create the key and generate a default value for it. This makes defaultdict a valuable option for handling missing keys in dictionaries.
I think this one-liner is a nearly perfect solution:
>>> from collections import defaultdict
>>> infinite_defaultdict = lambda: defaultdict(infinite_defaultdict)
>>> d = infinite_defaultdict()
>>> d['x']['y']['z'] = 10
by Raymond Hettinger on Twitter (https://twitter.com/raymondh/status/343823801278140417)
This lends itself naturally to a recursive definition.
>>> import collections
>>> def nested_dd():
... return collections.defaultdict(nested_dd)
...
>>> foo = nested_dd()
>>> foo
defaultdict(<function nested_dd at 0x023F0E30>, {})
>>> foo[1][2]=3
>>> foo[1]
defaultdict(<function nested_dd at 0x023F0E30>, {2: 3})
>>> foo[1][2]
3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With