Re: Extending dict (dict's) to allow for multidimensional dictionary

Discussion in 'Python' started by Ravi, Mar 5, 2011.

  1. Ravi

    Ravi Guest

    Ravi, Mar 5, 2011
    #1
    1. Advertising

  2. Ravi

    Peter Otten Guest

    Ravi wrote:

    > I found a solution here:
    >
    > http://parand.com/say/index.php/2007/07/13/simple-multi-dimensional-

    dictionaries-in-python/
    >
    > Please tell how good is it?


    Follow the link to the cookbook and read Andrew Dalke's comment ;)
    To spell it out:

    >>> class D(dict):

    .... def __missing__(self, key):
    .... result = self[key] = D()
    .... return result
    ....
    >>> d = D()
    >>> d["a"]["b"]["c"] = 42
    >>> d

    {'a': {'b': {'c': 42}}}

    This is not only simpler, it should also give you faster lookup when the key
    already exists.
     
    Peter Otten, Mar 5, 2011
    #2
    1. Advertising

  3. Ravi

    Javier Guest

    Looks a good idea. I use this kind of "recursive dicts" to represent
    tree like datastruct in python. Like:

    car["ford"]["taurus"]["price"]=...
    car["toyota"]["corolla"]["mpg"]=...
    car["toyota"]["corolla"]["price"]=...


    It would be good if it could be combined with class2dict (converting
    dict elemnets into attributes of a class/instance),

    http://vsbabu.org/mt/archives/2003/02/13/joy_of_python_classes_and_dictionaries.html

    so the former would look a bit nicer:

    car.toyota.corolla.mpg=...
    car.toyota.corolla.price=...

    Does anybody have another idea for representing tree-like data? Is
    there another type for trees in python?



    Ravi <> wrote:
    > I found a solution here:
    >
    > http://parand.com/say/index.php/2007/07/13/simple-multi-dimensional-dictionaries-in-python/
    >
    > Please tell how good is it?
     
    Javier, Mar 7, 2011
    #3
  4. Ravi

    Chris Rebert Guest

    On Sun, Mar 6, 2011 at 11:06 PM, Javier <> wrote:
    > Looks a good idea.  I use this kind of "recursive dicts" to represent
    > tree like datastruct in python.  Like:
    >
    > car["ford"]["taurus"]["price"]=...
    > car["toyota"]["corolla"]["mpg"]=...
    > car["toyota"]["corolla"]["price"]=...
    >

    <snip>
    > Does anybody have  another idea for representing tree-like data?  Is
    > there another type for trees in python?


    You see a tree, I see a database (http://docs.python.org/library/sqlite3.html ):

    +--------------+---------+-----+-------+
    | Manufacturer | Model | MPG | Price |
    +--------------+---------+-----+-------+
    | Ford | Taurus | ... | $... |
    | Toyota | Corolla | ... | $... |
    +--------------+---------+-----+-------+

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Mar 7, 2011
    #4
  5. Ravi

    sturlamolden Guest

    On 7 Mar, 09:30, Chris Rebert <> wrote:

    > You see a tree, I see a database (http://docs.python.org/library/sqlite3.html):
    >
    > +--------------+---------+-----+-------+
    > | Manufacturer | Model   | MPG | Price |
    > +--------------+---------+-----+-------+
    > | Ford         | Taurus  | ... | $...  |
    > | Toyota       | Corolla | ... | $...  |
    > +--------------+---------+-----+-------+


    If a tree of dictionaries becomes a database, then the Python
    interpreter itself becomes a database.

    Maybe we can implement Python on top of sqlite or Oracle? ;-)
     
    sturlamolden, Mar 7, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff
    Replies:
    2
    Views:
    970
    clintonG
    Sep 19, 2006
  2. Almad
    Replies:
    8
    Views:
    416
    Terry Reedy
    Dec 14, 2006
  3. Jonathan Fine
    Replies:
    9
    Views:
    368
    Jonathan Fine
    Jun 23, 2007
  4. Ravi
    Replies:
    3
    Views:
    281
  5. Ryan Taylor
    Replies:
    1
    Views:
    700
    Ryan Taylor
    Sep 9, 2004
Loading...

Share This Page