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

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

  1. Ravi

    Ravi Guest

    I can extend dictionary to allow for the my own special look-up tables. However now I want to be able to define multidimensional dictionary which supports look-up like this:

    d[1]['abc'][40] = 'dummy'

    and if d[1] and d[1][abc] raise KeyError just create them.

    for d[1] I can override __getitem__() but how to do the same for the d[1][abc]?
     
    Ravi, Mar 5, 2011
    #1
    1. Advertising

  2. Ravi

    Paul Rubin Guest

    Ravi <> writes:
    > I can extend dictionary to allow for the my own special look-up
    > tables. However now I want to be able to define multidimensional
    > dictionary which supports look-up like this:
    >
    > d[1]['abc'][40] = 'dummy'


    Why do that anyway? You can use a tuple as a subscript:

    d[1,'abc',40] = 'dummy'
     
    Paul Rubin, Mar 5, 2011
    #2
    1. Advertising

  3. Ravi

    John Nagle Guest

    On 3/5/2011 12:05 PM, Paul Rubin wrote:
    > Ravi<> writes:
    >> I can extend dictionary to allow for the my own special look-up
    >> tables. However now I want to be able to define multidimensional
    >> dictionary which supports look-up like this:
    >>
    >> d[1]['abc'][40] = 'dummy'

    >
    > Why do that anyway? You can use a tuple as a subscript:
    >
    > d[1,'abc',40] = 'dummy'


    Also, at some point, it's time to use a database.
    If you find yourself writing those "dictionaries" to
    files, or trying to look up everything with "abc"
    in the second subscript, a database is appropriate.

    John Nagle
     
    John Nagle, Mar 5, 2011
    #3
  4. Ravi

    TomF Guest

    On 2011-03-05 12:05:43 -0800, Paul Rubin said:

    > Ravi <> writes:
    >> I can extend dictionary to allow for the my own special look-up
    >> tables. However now I want to be able to define multidimensional
    >> dictionary which supports look-up like this:
    >>
    >> d[1]['abc'][40] = 'dummy'

    >
    > Why do that anyway? You can use a tuple as a subscript:
    >
    > d[1,'abc',40] = 'dummy'


    Depends on the use cases. If you only want to access d by a full
    tuple, that's fine. If you want to access all the entries below d[1],
    it's a problem. And if you want to access entities randomly by an
    arbitrary dimension, you probably want a database (as others have
    pointed out).

    -Tom
     
    TomF, Mar 7, 2011
    #4
    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:
    979
    clintonG
    Sep 19, 2006
  2. Almad
    Replies:
    8
    Views:
    423
    Terry Reedy
    Dec 14, 2006
  3. Jonathan Fine
    Replies:
    9
    Views:
    370
    Jonathan Fine
    Jun 23, 2007
  4. Ravi
    Replies:
    4
    Views:
    439
    sturlamolden
    Mar 7, 2011
  5. Ryan Taylor
    Replies:
    1
    Views:
    710
    Ryan Taylor
    Sep 9, 2004
Loading...

Share This Page