efficient updating of nested dictionaries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • omission9

    efficient updating of nested dictionaries

    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?



  • Edward C. Jones

    #2
    Re: efficient updating of nested dictionaries

    omission9 wrote:[color=blue]
    > 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?[/color]

    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".

    Comment

    • Rich Krauter

      #3
      Re: efficient updating of nested dictionaries

      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:
      [color=blue]
      > 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?
      >
      >[/color]

      Comment

      • omission9

        #4
        Re: efficient updating of nested dictionaries

        omission9 wrote:
        [color=blue]
        > 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?
        >
        >
        >[/color]
        So far I have found this on the internet:
        def rUpdate(self,ta rgetDict,itemDi ct):
        valtab=[]
        for key,val in itemDict.items( ):
        if type(val)==type ({}):
        newTarget=targe tDict.setdefaul t(key,{})
        self.rUpdate(ne wTarget,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?

        Comment

        • Josiah Carlson

          #5
          Re: efficient updating of nested dictionaries

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

          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

          Comment

          • Sidharthk

            #6
            Re: efficient updating of nested dictionaries

            omission9 <omission9@inva lid.email.info> wrote in message news:<H%_Qb.260 9$925.1917@nwrd dc02.gnilink.ne t>...[color=blue]
            > 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?[/color]

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

            unless you have a particular reason to use these nested dicts :)

            Comment

            • Sidharthk

              #7
              Re: efficient updating of nested dictionaries

              omission9 <omission9@inva lid.email.info> wrote in message news:<H%_Qb.260 9$925.1917@nwrd dc02.gnilink.ne t>...[color=blue]
              > 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?[/color]

              Use Tuples

              MY_DICT[(KEY_X,KEY_Y,KE Y_Z)]=FOO

              Unless for some you need to use nested dicts :)

              Comment

              • Sidharthk

                #8
                Re: efficient updating of nested dictionaries

                omission9 <omission9@inva lid.email.info> wrote in message news:<H%_Qb.260 9$925.1917@nwrd dc02.gnilink.ne t>...[color=blue]
                > 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?[/color]

                Use Tuples

                MY_DICT[(KEY_X,KEY_Y,KE Y_Z)]=FOO

                Unless for some you need to use nested dicts :)

                Comment

                • Duncan Booth

                  #9
                  Re: efficient updating of nested dictionaries

                  Josiah Carlson <jcarlson@nospa m.uci.edu> wrote in
                  news:bv2e21$j5e $2@news.service .uci.edu:
                  [color=blue][color=green]
                  >> 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[/color]
                  >
                  > 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.[/color]

                  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)

                  Comment

                  • Sidharthk

                    #10
                    Re: efficient updating of nested dictionaries

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

                    def rec_update(mydi ct, newdict):
                    presentKeysPair s = [(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 presentKeysPair s:
                    currentValue = mydict[key]
                    if isisntance(newV alue, dict):
                    mydict[key] = rec_update(newV alue)
                    else:
                    mydict[key] = newValue
                    mydict.update(d ict(newKeysPair s))
                    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?

                    Comment

                    • Rich Krauter

                      #11
                      Re: efficient updating of nested dictionaries

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


                      What a nice way to simplify this common task. That's great. Thanks for
                      the advice.
                      Rich

                      Comment

                      • Ben Finney

                        #12
                        Re: efficient updating of nested dictionaries

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

                        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/>

                        Comment

                        • Rich Krauter

                          #13
                          Re: efficient updating of nested dictionaries

                          Oh crap. Sorry about the html emails. I've been meaning to turn that
                          off. Thanks for reminding me.
                          Rich

                          Comment

                          • Ben Finney

                            #14
                            Re: efficient updating of nested dictionaries

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

                            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/>

                            Comment

                            Working...