[Tutor] creating dict of dict : similar to perl hash of hash
Jerry Hill
malaclypse2 at gmail.com
Wed Mar 7 05:01:12 CET 2012
On Tue, Mar 6, 2012 at 6:21 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> But it doesn't have the same dynamic depth that the Perl version has, you
> need to know your maximum depth. The last level will always have the default
> set to None.
>
> You could create a class subclassed from defaultdict that would do it
> though...
It's a pretty easy class to write:
from collections import defaultdict
class recursivedefaultdict(defaultdict):
def __init__(self):
self.default_factory = type(self)
Given that bit of code, the OP's example looks a bit like this:
a = 1
b = 2
c = 'apples'
my_hash = recursivedefaultdict()
my_hash[a][b][c] = "value"
if my_hash[a][b][c]:
print("found value")
--
Jerry
More information about the Tutor
mailing list