Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
defaultdict of arbitrary depth
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Paul McGuire  
View profile  
 More options Aug 16 2007, 11:25 pm
Newsgroups: comp.lang.python
From: Paul McGuire <pt...@austin.rr.com>
Date: Thu, 16 Aug 2007 20:25:13 -0700
Local: Thurs, Aug 16 2007 11:25 pm
Subject: defaultdict of arbitrary depth
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carsten Haese  
View profile  
 More options Aug 17 2007, 12:19 am
Newsgroups: comp.lang.python
From: Carsten Haese <cars...@uniqsys.com>
Date: Fri, 17 Aug 2007 00:19:13 -0400
Local: Fri, Aug 17 2007 12:19 am
Subject: Re: defaultdict of arbitrary depth

This is shorter:

from collections import defaultdict

class recursivedefaultdict(defaultdict):
    def __init__(self):
        self.default_factory = type(self)

--
Carsten Haese
http://informixdb.sourceforge.net


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paul McGuire  
View profile  
 More options Aug 17 2007, 12:27 am
Newsgroups: comp.lang.python
From: Paul McGuire <pt...@austin.rr.com>
Date: Thu, 16 Aug 2007 21:27:11 -0700
Local: Fri, Aug 17 2007 12:27 am
Subject: Re: defaultdict of arbitrary depth
On Aug 16, 11:19 pm, Carsten Haese <cars...@uniqsys.com> wrote:

Of course, very short and sweet!  Any special reason you wrote:
        self.default_factory = type(self)
instead of:
        self.default_factory = recursivedefaultdict
?

-- Paul


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carsten Haese  
View profile  
 More options Aug 17 2007, 12:53 am
Newsgroups: comp.lang.python
From: Carsten Haese <cars...@uniqsys.com>
Date: Fri, 17 Aug 2007 00:53:12 -0400
Local: Fri, Aug 17 2007 12:53 am
Subject: Re: defaultdict of arbitrary depth

On Thu, 2007-08-16 at 21:27 -0700, Paul McGuire wrote:
> Of course, very short and sweet!  Any special reason you wrote:
>         self.default_factory = type(self)
> instead of:
>         self.default_factory = recursivedefaultdict
> ?

Besides a pathological need to be clever? ;) The former keeps the
recursive relationship intact if you define a class that inherits from
recursivedefaultdict. The latter would create the nested entries of the
derived class as "vanilla" recursivedefaultdicts instead of instances of
the derived class. Whether this would matter in practice is, of course,
a different question.

--
Carsten Haese
http://informixdb.sourceforge.net


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steve Holden  
View profile  
 More options Aug 17 2007, 1:40 am
Newsgroups: comp.lang.python
From: Steve Holden <st...@holdenweb.com>
Date: Fri, 17 Aug 2007 01:40:35 -0400
Local: Fri, Aug 17 2007 1:40 am
Subject: Re: defaultdict of arbitrary depth

It's more robust under subclassing.

regards
  Steve
--
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel  
View profile  
 More options Aug 21 2007, 8:49 pm
Newsgroups: comp.lang.python
From: Daniel <miller...@gmail.com>
Date: Tue, 21 Aug 2007 17:49:51 -0700
Local: Tues, Aug 21 2007 8:49 pm
Subject: Re: defaultdict of arbitrary depth
Any reason why this wouldn't work?

>>> from collections import defaultdict
>>> def rdict(*args, **kw):

...     return defaultdict(rdict, *args, **kw)
...
>>> d = rdict()
>>> d[1][2][3][4][5] # ...

defaultdict(<function rdict at 0x61370>, {})

~ Daniel


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Paddy  
View profile  
 More options Aug 22 2007, 12:38 am
Newsgroups: comp.lang.python
From: Paddy <paddy3...@googlemail.com>
Date: Tue, 21 Aug 2007 21:38:43 -0700
Local: Wed, Aug 22 2007 12:38 am
Subject: Re: defaultdict of arbitrary depth
On Aug 17, 4:25 am, Paul McGuire <pt...@austin.rr.com> wrote:

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

- Paddy.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google