efficient updating of nested dictionaries

Discussion in 'Python' started by omission9, Jan 26, 2004.

  1. omission9

    omission9 Guest

    I have a dictionary that looks like this
    MY_DICT[KEY_X][KEY_Y][KEY_Z]=FOO

    I am having a problem updating this with a simple
    MY_DICT.update(NEW_DICT) as update doesn't seem to care about getting
    into the inner dicts.
    Getting the keys of each and iterating through and updating each one is
    terribly slow as the number of keys gets bigger and bigger.
    What is the bst way to update my nested dicts?
    omission9, Jan 26, 2004
    #1
    1. Advertising

  2. omission9 wrote:
    > I have a dictionary that looks like this
    > MY_DICT[KEY_X][KEY_Y][KEY_Z]=FOO
    >
    > I am having a problem updating this with a simple
    > MY_DICT.update(NEW_DICT) as update doesn't seem to care about getting
    > into the inner dicts.
    > Getting the keys of each and iterating through and updating each one is
    > terribly slow as the number of keys gets bigger and bigger.
    > What is the bst way to update my nested dicts?


    Make a table whose rows are (KEY_X, KEY_Y, KEY_Z, FOO). If the table is
    large use MySQL or some other database. For small or medium sized tables
    try "http://members.tripod.com/~edcjones/MultiDict.py".
    Edward C. Jones, Jan 26, 2004
    #2
    1. Advertising

  3. omission9

    Rich Krauter Guest

    The following is probably too dependent on the data type of the keys,
    but it may be suitable in some programs. It's certainly not a general
    solution for all cases. Others will have much better ideas, but here
    goes anyway ...

    You may want to use a non-nested dict with a 'superkey' composed of the
    concatenation of the three keys, seperated by some delimiter.
    use MY_DICT[KEY_X+'_'+KEY_Y+'_'+KEY_Z]=FOO

    Then you could use update().You would just have to do some pre- and
    post-processing of the keys. i.e. splitting or joining the 'superkey' by
    the delimiter you choose.

    Although, that's probably kind of lame - I bet others will have much
    better suggestions. I'm interested in how other people do this too.
    Rich


    On Sun, 2004-01-25 at 21:33, omission9 wrote:

    > I have a dictionary that looks like this
    > MY_DICT[KEY_X][KEY_Y][KEY_Z]=FOO
    >
    > I am having a problem updating this with a simple
    > MY_DICT.update(NEW_DICT) as update doesn't seem to care about getting
    > into the inner dicts.
    > Getting the keys of each and iterating through and updating each one is
    > terribly slow as the number of keys gets bigger and bigger.
    > What is the bst way to update my nested dicts?
    >
    >
    Rich Krauter, Jan 26, 2004
    #3
  4. omission9

    omission9 Guest

    omission9 wrote:

    > I have a dictionary that looks like this
    > MY_DICT[KEY_X][KEY_Y][KEY_Z]=FOO
    >
    > I am having a problem updating this with a simple
    > MY_DICT.update(NEW_DICT) as update doesn't seem to care about getting
    > into the inner dicts.
    > Getting the keys of each and iterating through and updating each one is
    > terribly slow as the number of keys gets bigger and bigger.
    > What is the bst way to update my nested dicts?
    >
    >
    >

    So far I have found this on the internet:
    def rUpdate(self,targetDict,itemDict):
    valtab=[]
    for key,val in itemDict.items():
    if type(val)==type({}):
    newTarget=targetDict.setdefault(key,{})
    self.rUpdate(newTarget,val)
    else:
    targetDict[key]=val

    However, this does not seem to handle the fact that each dict has
    multiple keys. :( So far the modification I have made to make it work
    right have failed. Any ideas?
    omission9, Jan 26, 2004
    #4
  5. > Although, that's probably kind of lame - I bet others will have much
    > better suggestions. I'm interested in how other people do this too.
    > Rich


    String concatenation is not that lame, but I'd use tuples:
    MY_DICT[(KEY_X, KEY_Y, KEY_Z)] = FOO

    Tuples save on string operations.

    - Josiah
    Josiah Carlson, Jan 26, 2004
    #5
  6. omission9

    Sidharthk Guest

    omission9 <> wrote in message news:<H%_Qb.2609$>...
    > I have a dictionary that looks like this
    > MY_DICT[KEY_X][KEY_Y][KEY_Z]=FOO
    >
    > I am having a problem updating this with a simple
    > MY_DICT.update(NEW_DICT) as update doesn't seem to care about getting
    > into the inner dicts.
    > Getting the keys of each and iterating through and updating each one is
    > terribly slow as the number of keys gets bigger and bigger.
    > What is the bst way to update my nested dicts?


    Use a tuple
    MY_DICT[(KEY_X,KEY_Y,KEY_Z)]=FOO

    unless you have a particular reason to use these nested dicts :)
    Sidharthk, Jan 26, 2004
    #6
  7. omission9

    Sidharthk Guest

    omission9 <> wrote in message news:<H%_Qb.2609$>...
    > I have a dictionary that looks like this
    > MY_DICT[KEY_X][KEY_Y][KEY_Z]=FOO
    >
    > I am having a problem updating this with a simple
    > MY_DICT.update(NEW_DICT) as update doesn't seem to care about getting
    > into the inner dicts.
    > Getting the keys of each and iterating through and updating each one is
    > terribly slow as the number of keys gets bigger and bigger.
    > What is the bst way to update my nested dicts?


    Use Tuples

    MY_DICT[(KEY_X,KEY_Y,KEY_Z)]=FOO

    Unless for some you need to use nested dicts :)
    Sidharthk, Jan 26, 2004
    #7
  8. omission9

    Sidharthk Guest

    omission9 <> wrote in message news:<H%_Qb.2609$>...
    > I have a dictionary that looks like this
    > MY_DICT[KEY_X][KEY_Y][KEY_Z]=FOO
    >
    > I am having a problem updating this with a simple
    > MY_DICT.update(NEW_DICT) as update doesn't seem to care about getting
    > into the inner dicts.
    > Getting the keys of each and iterating through and updating each one is
    > terribly slow as the number of keys gets bigger and bigger.
    > What is the bst way to update my nested dicts?


    Use Tuples

    MY_DICT[(KEY_X,KEY_Y,KEY_Z)]=FOO

    Unless for some you need to use nested dicts :)
    Sidharthk, Jan 26, 2004
    #8
  9. omission9

    Duncan Booth Guest

    Josiah Carlson <> wrote in
    news:bv2e21$j5e$:

    >> Although, that's probably kind of lame - I bet others will have much
    >> better suggestions. I'm interested in how other people do this too.
    >> Rich

    >
    > String concatenation is not that lame, but I'd use tuples:
    > MY_DICT[(KEY_X, KEY_Y, KEY_Z)] = FOO
    >
    > Tuples save on string operations.


    I would omit the extra parentheses here, but its a style thing.

    MY_DICT[KEY_X, KEY_Y, KEY_Z] = FOO

    (Note to original poster: I'd also turn off caps-lock)
    Duncan Booth, Jan 26, 2004
    #9
  10. omission9

    Sidharthk Guest

    This is untested code but i think it should work.(fingers crossed)
    Btw i doubt this will be fast though.

    def rec_update(mydict, newdict):
    presentKeysPairs = [(key,value)
    for (key, value) in newdict.items()
    if mydict.has_key(key)]
    newKeysPairs = [(key,value)
    for (key, value) in newdict,items()
    if not mydict.has_key(key)]
    for key, newValue in presentKeysPairs:
    currentValue = mydict[key]
    if isisntance(newValue, dict):
    mydict[key] = rec_update(newValue)
    else:
    mydict[key] = newValue
    mydict.update(dict(newKeysPairs))
    return mydict

    regards

    ps. why can't you simply use tuples to represent the different
    dimensions, even if the number of dimensions vary.
    is there any particular reason why you are using these nested
    dictionaries?
    Sidharthk, Jan 26, 2004
    #10
  11. omission9

    Rich Krauter Guest

    >> String concatenation is not that lame, but I'd use tuples:
    >> MY_DICT[(KEY_X, KEY_Y, KEY_Z)] = FOO
    >> Tuples save on string operations.



    What a nice way to simplify this common task. That's great. Thanks for
    the advice.
    Rich
    Rich Krauter, Jan 27, 2004
    #11
  12. omission9

    Ben Finney Guest

    On Mon, 26 Jan 2004 20:11:39 -0500, Rich Krauter wrote:
    > What a nice way to simplify this common task. That's great. Thanks for
    > the advice.
    >
    > [HTML garbage repeating the same content]


    What a hideous way to complicate this simple medium. That sucks.
    Thanks for turning it off in future.

    --
    \ "My roommate got a pet elephant. Then it got lost. It's in the |
    `\ apartment somewhere." -- Steven Wright |
    _o__) |
    Ben Finney <http://bignose.squidly.org/>
    Ben Finney, Jan 27, 2004
    #12
  13. omission9

    Rich Krauter Guest

    Oh crap. Sorry about the html emails. I've been meaning to turn that
    off. Thanks for reminding me.
    Rich
    Rich Krauter, Jan 27, 2004
    #13
  14. omission9

    Ben Finney Guest

    On Mon, 26 Jan 2004 21:32:28 -0500, Rich Krauter wrote:
    > Oh crap. Sorry about the html emails. I've been meaning to turn that
    > off. Thanks for reminding me.


    Much better! Thanks for being considerate.

    --
    \ "When I turned two I was really anxious, because I'd doubled my |
    `\ age in a year. I thought, if this keeps up, by the time I'm six |
    _o__) I'll be ninety." -- Steven Wright |
    Ben Finney <http://bignose.squidly.org/>
    Ben Finney, Jan 27, 2004
    #14
    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. Narendra C. Tulpule

    Are lists at least as efficient as dictionaries?

    Narendra C. Tulpule, Aug 29, 2003, in forum: Python
    Replies:
    6
    Views:
    321
    Chad Netzer
    Aug 30, 2003
  2. David Pratt

    Efficient lookup in list of dictionaries

    David Pratt, Dec 5, 2005, in forum: Python
    Replies:
    2
    Views:
    295
    bruno at modulix
    Dec 5, 2005
  3. lysdexia
    Replies:
    6
    Views:
    490
    John Machin
    Dec 2, 2007
  4. news.rcn.com
    Replies:
    2
    Views:
    1,112
    Roedy Green
    Dec 10, 2007
  5. Brandon
    Replies:
    12
    Views:
    485
    Brandon
    Aug 15, 2008
Loading...

Share This Page