Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > efficient updating of nested dictionaries

Reply
Thread Tools

efficient updating of nested dictionaries

 
 
omission9
Guest
Posts: n/a
 
      01-26-2004
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?



 
Reply With Quote
 
 
 
 
Edward C. Jones
Guest
Posts: n/a
 
      01-26-2004
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".
 
Reply With Quote
 
 
 
 
Rich Krauter
Guest
Posts: n/a
 
      01-26-2004
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?
>
>


 
Reply With Quote
 
omission9
Guest
Posts: n/a
 
      01-26-2004
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?

 
Reply With Quote
 
Josiah Carlson
Guest
Posts: n/a
 
      01-26-2004
> 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
 
Reply With Quote
 
Sidharthk
Guest
Posts: n/a
 
      01-26-2004
omission9 <(E-Mail Removed)> wrote in message news:<H%_Qb.2609$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Sidharthk
Guest
Posts: n/a
 
      01-26-2004
omission9 <(E-Mail Removed)> wrote in message news:<H%_Qb.2609$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Sidharthk
Guest
Posts: n/a
 
      01-26-2004
omission9 <(E-Mail Removed)> wrote in message news:<H%_Qb.2609$(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      01-26-2004
Josiah Carlson <(E-Mail Removed)> wrote in
news:bv2e21$j5e$(E-Mail Removed):

>> 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)
 
Reply With Quote
 
Sidharthk
Guest
Posts: n/a
 
      01-26-2004
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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
updating dictionaries from/to dictionaries Brandon Python 12 08-15-2008 12:35 AM
Pickling dictionaries containing dictionaries: failing,recursion-style! lysdexia Python 6 12-02-2007 12:03 AM
Efficient lookup in list of dictionaries David Pratt Python 2 12-05-2005 09:31 AM
dictionaries with nested lists Alex Martelli Python 6 11-02-2003 09:27 AM
Are lists at least as efficient as dictionaries? Narendra C. Tulpule Python 6 08-30-2003 08:20 PM



Advertisments