Note that there are some explanatory texts on larger screens.

plurals
  1. POchanging value in nested dictionary python using class
    primarykey
    data
    text
    <pre><code>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) </code></pre> <p>It is a class warehouse I want to create a dictionary which I did by the calling the <strong>init</strong> methods of the class.Now I want to store some values into the nested dictionary using same instance of class warehouse.When I call the obj.store(2,34,4,44).It updates the dictionary and gimme the result.</p> <pre><code>{'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} } } </code></pre> <p>But I was expecting :</p> <pre><code>{'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} } } </code></pre> <p>It sets the value in all the other nested dictionary of key 'A' and 1 I try to put PDB and debuge it, but it is showing the same result. But If I do this operation in teminal then I get the result what I was expecting.</p> <pre><code>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. &gt;&gt;&gt; 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}}} &gt;&gt;&gt; d['A'][1][1]={"some_key":"some_value",} &gt;&gt;&gt; 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}}} </code></pre> <p>I dont know maybe I am missing something, or there is something wrong that I am not able to catch up.I am using python 2.6.6 and tried this is 2.7.1 as well .</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload