efficient updating of nested dictionaries

O

omission9

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?
 
E

Edward C. Jones

omission9 said:
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".
 
R

Rich Krauter

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
 
O

omission9

omission9 said:
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?
 
J

Josiah Carlson

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
 
S

Sidharthk

omission9 said:
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 :)
 
S

Sidharthk

omission9 said:
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 :)
 
S

Sidharthk

omission9 said:
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 :)
 
D

Duncan Booth

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)
 
S

Sidharthk

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?
 
B

Ben Finney

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

Rich Krauter

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top