defaultdict of arbitrary depth

Paddy paddy3118 at googlemail.com
Wed Aug 22 00:38:43 EDT 2007


On Aug 17, 4:25 am, Paul McGuire <pt... at austin.rr.com> wrote:
> In responding to another post on defaultdict, I posted an
> implementation of a 2-level hashtable, by creating a factory method
> that returned a defaultdict(dict).  The OP of that other thread was
> trying to build a nested tree from a set of n-tuples, in which the
> first (n-1) values in each tuple were the keys for navigating down the
> tree, and the final n'th value was the value for to be assigned to the
> leaf node.  My post worked only if n=2, which fortunately was the test
> case that the OP gave.  But it annoyed me that this required advance
> knowledge of the number of keys.
>
> I've hacked out this recursivedefaultdict which is a
> defaultdict(defaultdict(defaultdict(...))), arbitrarily deep depending
> on the keys provided in the reference.
>
> Please comment.
>
> -- Paul
>
> from collections import defaultdict
>
> data = [
>     ('A','B','Z',1), ('A','C','Y',2), ('A','C','X',3),
>     ('B','A','W',4), ('B','B','V',5), ('B','B','U',6),
>     ('B','D','T',7),
>     ]
>
> class recursivedefaultdict(object):
>     def __init__(self):
>         self.__dd = defaultdict(recursivedefaultdict)
>     def __getattr__(self,attr):
>         return self.__dd.__getattribute__(attr)
>     def __getitem__(self,*args):
>         return self.__dd.__getitem__(*args)
>     def __setitem__(self,*args):
>         return self.__dd.__setitem__(*args)
>
> table = recursivedefaultdict()
>
> for k1,k2,k3,v in data:
>     table[k1][k2][k3] = v
>
> for kk in sorted(table.keys()):
>     print "-",kk
>     for jj in sorted(table[kk].keys()):
>         print "  -",jj
>         for ii in sorted(table[kk][jj].keys()):
>             print "    -",ii,table[kk][jj][ii]
>
> prints:
> - A
>   - B
>     - Z 1
>   - C
>     - X 3
>     - Y 2
> - B
>   - A
>     - W 4
>   - B
>     - U 6
>     - V 5
>   - D
>     - T 7

There is something here:
  http://groups.google.com/group/comp.lang.python/msg/92502e0278c2a56e

- Paddy.




More information about the Python-list mailing list