且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

有效更新嵌套字典

更新时间:2023-11-27 13:51:04

omission9写道:
omission9 wrote:
我有一本看起来像这样的词典
MY_DICT [KEY_X] [KEY_Y] [KEY_Z] = FOO

我在使用简单的MY_DICT.update(NEW_DICT)更新时出现问题,因为更新似乎并不关心获取进入内部词汇。
获取每个键的键并迭代并更新每个键是非常慢的,因为键的数量变得越来越大。
更新嵌套dicts的***方法是什么?
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?




创建一个行(KEY_X,KEY_Y,KEY_Z,FOO)的表。如果表是

大使用MySQL或其他一些数据库。对于中小型桌子

试试http://members.tripod.com/~edcjones/MultiDict.py"。



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


以下可能过于依赖于键的数据类型,

但它可能适用于某些程序。对于所有情况,它肯定不是一般的

解决方案。其他人会有更好的想法,但是这里

无论如何...


您可能想要使用带有''超级键'的非嵌套字典由三个键组成的

连接,由一些分隔符分隔。

使用MY_DICT [KEY_X +''_''+ KEY_Y +''_''+ KEY_Z] = FOO


然后你可以使用update()。你只需要对密钥进行一些预处理和后期处理。即分裂或加入''超级键''

您选择的分隔符。


虽然,这可能有点蹩脚 - 我敢打赌其他人将有更多的建议更好的建议。我也很感兴趣其他人也这样做。

Rich

On Sun,2004-01-25 at 21:33,omission9写道:
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:
我有一个看起来像这样的字典
MY_DICT [KEY_X] [KEY_Y] [KEY_Z] = FOO

我在用简单的方法更新这个问题时遇到了问题
MY_DICT.update(NEW_DICT)因为更新似乎并不关心内部词汇。
获取每个词的键并迭代并更新每个词是/ >当键的数量变得越来越大时,速度非常慢。
更新嵌套dicts的***方法是什么?
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写道:
omission9 wrote:
我有一个看起来像这样的字典
MY_DICT [KEY_X] [KEY_Y] [KEY_Z] = FOO

获取k随着键的数量变得越来越大,每个的eys和迭代并更新每个键都非常慢。
更新嵌套dicts的***方法是什么?
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 >

但是,这似乎没有处理每个字典有多个键的
这一事实。 :(到目前为止,我已经做了修改使其工作

正确失败。有任何想法吗?


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?