使用类更改嵌套字典python中的值

时间:2012-09-24 21:30:04

标签: python object dictionary hashmap associative-array

class warehouse:

    def __init__(self):
        self.A={}
        self.B={}   
        self.racks={'A':self.initialize(self.A),'B':self.initialize(self.B)}        

    def initialize(self,rack):
        shelf = dict([  (items,0) for items in range(1,6)  ])
        for x in range(3):
            rack[x+1]=shelf 
        return rack 

    def store(self,_id,owner_id,colour,weigth):
        import pdb;pdb.set_trace()          
        empty_position=self.empty_positions(self.store.__name__)[0]     
        self.racks[empty_position[0]][empty_position[1]][empty_position[2]]=   {'id':_id,'owner':owner_id,'colour':colour,'weigth':weigth}          
        print self.racks
    def empty_positions(self,name):

        store_list=[]
        for rack,shelfs in self.racks.iteritems():
            for shelf_number,shelf_objects in shelfs.iteritems():
                    store_list.append([rack,shelf_number])
                    for position,value in shelf_objects.iteritems():
                        if 0==value:
                            store_list.append([rack,shelf_number,position])

        return store_list

obj=warehouse()
val=obj.store(2,34,4,44)                

这是一个类仓库我想创建一个字典,我通过调用类的 init 方法来完成。现在我想使用相同的类实例将一些值存储到嵌套字典中仓库。当我调用obj.store(2,34,4,44)时。它会更新字典并给结果带来好处。

{'A': {1: {1: {'colour': 4, 'id': 2, 'owner': 34, 'weigth': 44},
           2: 0,
           3: 0,
           4: 0,
           5: 0},
       2: {1: {'colour': 4, 'id': 2, 'owner': 34, 'weigth': 44},
           2: 0,
           3: 0,
           4: 0,
           5: 0},
       3: {1: {'colour': 4, 'id': 2, 'owner': 34, 'weigth': 44},
           2: 0,
           3: 0,
           4: 0,
           5: 0}},
'B': {1: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
      2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
      3: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}
     }
 }

但我在期待:

{'A': {1: {1: {'colour': 4, 'id': 2, 'owner': 34, 'weigth': 44},
           2: 0,
           3: 0,
           4: 0,
           5: 0},
       2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
       3: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}},
 'B': {1: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
       2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
       3: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}
      }
 }

它设置键'A'和1的所有其他嵌套字典中的值 我尝试将PDB和debuge它,但它显示相同的结果。 但如果我在终端中进行此操作,那么我得到的结果就是我所期待的。

Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d={'A': {1: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}, 2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}, 3: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}}, 'B': {1: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}, 2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}, 3: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}}}
>>> d['A'][1][1]={"some_key":"some_value",}
>>> d
{'A': {1: {1: {'some_key': 'some_value'}, 2: 0, 3: 0, 4: 0, 5: 0}, 2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}, 3: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}}, 'B': {1: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}, 2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}, 3: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0}}}

我不知道也许我错过了什么,或者有些错误我无法赶上。我正在使用python 2.6.6并试过这个也是2.7.1。

2 个答案:

答案 0 :(得分:4)

当你这样做时:

shelf = dict([  (items,0) for items in range(1,6)  ])
for x in range(3):
    rack[x+1]=shelf 

您正在创建一个架子,然后将相同的架子放入机架中3次。货架不是副本,它们是同一个

每次都需要创建一个新架子:

def initialize(self, rack):
    for x in range(3):
        rack[x+1] = dict((items,0) for items in range(1,6))
    return rack

在python 2.7中,使用字典理解,这可以成为:

def initialize(self, rack):
    rack.update({
        x + 1 : { item : 0 for item in range(1, 6) } for x in range(3)
    })
    return rack

答案 1 :(得分:2)

您的问题出在initialize()方法中,特别是rack[x+1]=shelf

def initialize(self,rack):
    shelf = dict([  (items,0) for items in range(1,6)  ])
    for x in range(3):
        rack[x+1]=shelf 
    return rack 

rack[x+1]=shelf似乎无害,但事实并非如此。 rack将包含对同一shelf个对象的多个引用,这会产生奇怪的结果:

>>> a = {1: 2}
>>> b = [a for i in range(3)]
>>> b
[{1: 2}, {1: 2}, {1: 2}]
>>> b[0][1] = 3
>>> b
[{1: 3}, {1: 3}, {1: 3}]

请注意,当我更改了a的单个实例时,所有的所有都会受到影响。他们都指向同一个对象。

要解决此问题,请将rack[x+1]=shelf更改为rack[x+1]=dict(shelf)。现在,您迫使Python创建一个单独的shelf实例。