By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,395 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

efficient updating of nested dictionaries

P: n/a
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?

Jul 18 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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".
Jul 18 '05 #2

P: n/a
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?


Jul 18 '05 #3

P: n/a
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?

Jul 18 '05 #4

P: n/a
> 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
Jul 18 '05 #5

P: n/a
omission9 <om*******@invalid.email.info> wrote in message news:<H%*****************@nwrddc02.gnilink.net>...
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 :)
Jul 18 '05 #6

P: n/a
omission9 <om*******@invalid.email.info> wrote in message news:<H%*****************@nwrddc02.gnilink.net>...
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 :)
Jul 18 '05 #7

P: n/a
omission9 <om*******@invalid.email.info> wrote in message news:<H%*****************@nwrddc02.gnilink.net>...
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 :)
Jul 18 '05 #8

P: n/a
Josiah Carlson <jc******@nospam.uci.edu> wrote in
news:bv**********@news.service.uci.edu:
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)
Jul 18 '05 #9

P: n/a
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?
Jul 18 '05 #10

P: n/a
>> 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

Jul 18 '05 #11

P: n/a
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/>
Jul 18 '05 #12

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

Jul 18 '05 #13

P: n/a
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/>
Jul 18 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.