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? 13 5325
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".
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?
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?
> 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
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 :)
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 :)
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 :)
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)
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?
>> 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
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/>
Oh crap. Sorry about the html emails. I've been meaning to turn that
off. Thanks for reminding me.
Rich
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/> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Narendra C. Tulpule |
last post by:
Hi,
if you know the Python internals, here is a newbie question for you.
If I have a list with 100 elements, each element being a long string,
is...
|
by: Andy Baker |
last post by:
Hi there,
I'm learning Python at the moment and trying to grok the thinking behind
it's scoping and nesting rules.
I was googling for nested...
|
by: David Pratt |
last post by:
Hi. I like working with lists of dictionaries since order is preserved
in a list when I want order and the dictionaries make it explicit what
I...
|
by: techiepundit |
last post by:
I'm parsing some data of the form:
OuterName1 InnerName1=5,InnerName2=7,InnerName3=34;
OuterName2 InnerNameX=43,InnerNameY=67,InnerName3=21;...
|
by: Brian L. Troutwine |
last post by:
I've got a problem that I can't seem to get my head around and hoped
somebody might help me out a bit:
I've got a dictionary, A, that is...
|
by: gonzlobo |
last post by:
Greetings, and happyNewYear to all.
I picked up Python a few weeks ago, and have been able to parse large
files and process data pretty easily,...
|
by: Matthew Schibler |
last post by:
I'm a newbie to Python, with some experience using perl (where I used
nested arrays and hashes extensively). I am building a script in
python for a...
|
by: Brandon |
last post by:
Hi all,
I am not altogether experienced in Python, but I haven't been able to
find a good example of the syntax that I'm looking for in any...
|
by: Edwin.Madari |
last post by:
by the way, iterating over bar will throw KeyError if that key does not exist in foo. to see that in action, simply set another key in bar after...
|
by: tammygombez |
last post by:
Hey fellow JavaFX developers,
I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
|
by: tammygombez |
last post by:
Hey everyone!
I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
|
by: concettolabs |
last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
|
by: better678 |
last post by:
Question:
Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct?
Answer:
Java is an object-oriented...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
|
by: CD Tom |
last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
|
by: CD Tom |
last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
| |