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?
[Python] efficient updating of nested dictionaries
| Tweet |
|
Search Discussions
-
Edward C. Jones at Jan 26, 2004 at 3:33 am ⇧
Make a table whose rows are (KEY_X, KEY_Y, KEY_Z, FOO). If the table isomission9 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?
large use MySQL or some other database. For small or medium sized tables
try "http://members.tripod.com/~edcjones/MultiDict.py".
-
Rich Krauter at Jan 26, 2004 at 3:37 am ⇧
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.
RichOn Sun, 2004-01-25 at 21:33, omission9 wrote:-------------- next part --------------
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?
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-list/attachments/20040125/60ffed61/attachment.html -
Omission9 at Jan 26, 2004 at 5:03 am ⇧
So far I have found this on the internet: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?
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?
-
Sidharthk at Jan 26, 2004 at 10:44 am ⇧
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?
-
Josiah Carlson at Jan 26, 2004 at 7:00 am ⇧
-
Duncan Booth at Jan 26, 2004 at 9:39 am ⇧
Josiah Carlson <jcarlson at nospam.uci.edu> wrote in
news:bv2e21$j5e$2 at news.service.uci.edu:I would omit the extra parentheses here, but its a style thing.Although, that's probably kind of lame - I bet others will have muchString concatenation is not that lame, but I'd use tuples:
better suggestions. I'm interested in how other people do this too.
Rich
MY_DICT[(KEY_X, KEY_Y, KEY_Z)] = FOO
Tuples save on string operations.
MY_DICT[KEY_X, KEY_Y, KEY_Z] = FOO
(Note to original poster: I'd also turn off caps-lock)
-
Rich Krauter at Jan 27, 2004 at 1:11 am ⇧
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-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-list/attachments/20040126/2eba9fc8/attachment.htm -
Ben Finney at Jan 27, 2004 at 1:49 am ⇧
What a hideous way to complicate this simple medium. That sucks.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]
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/> -
Rich Krauter at Jan 27, 2004 at 2:32 am ⇧
-
Ben Finney at Jan 27, 2004 at 2:45 am ⇧
Much better! Thanks for being considerate.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.
--
\ "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/> -
Sidharthk at Jan 26, 2004 at 8:04 am ⇧
omission9 <omission9 at invalid.email.info> wrote in message news:<H%_Qb.2609$925.1917 at nwrddc02.gnilink.net>...I have a dictionary that looks like thisUse a tuple
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?
MY_DICT[(KEY_X,KEY_Y,KEY_Z)]=FOO
unless you have a particular reason to use these nested dicts :)
-
Sidharthk at Jan 26, 2004 at 9:01 am ⇧
omission9 <omission9 at invalid.email.info> wrote in message news:<H%_Qb.2609$925.1917 at nwrddc02.gnilink.net>...I have a dictionary that looks like thisUse Tuples
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?
MY_DICT[(KEY_X,KEY_Y,KEY_Z)]=FOO
Unless for some you need to use nested dicts :)
-
Sidharthk at Jan 26, 2004 at 9:01 am ⇧
omission9 <omission9 at invalid.email.info> wrote in message news:<H%_Qb.2609$925.1917 at nwrddc02.gnilink.net>...I have a dictionary that looks like thisUse Tuples
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?
MY_DICT[(KEY_X,KEY_Y,KEY_Z)]=FOO
Unless for some you need to use nested dicts :)
Related Discussions
Discussion Navigation
| view | thread | post |
Discussion Overview
| group | python-list |
| categories | python |
| posted | Jan 26, '04 at 2:33a |
| active | Jan 27, '04 at 2:45a |
| posts | 14 |
| users | 7 |
| website | python.org |
