Hi, I'm trying to work with a dictionary of dictionaries and I'm having trouble accessing a specific element of it:

$ python
Python 2.6 (trunk:66714:66715M, Oct  1 2008, 18:36:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5370)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> r = ('a','b','c')
>>> c = (1,2,3)
>>> d = dict.fromkeys(r,dict.fromkeys(c))
>>> d
{'a': {1: None, 2: None, 3: None}, 'c': {1: None, 2: None, 3: None}, 'b': {1: None, 2: None, 3: None}}
>>> d['a'][1]=0
>>> d
{'a': {1: 0, 2: None, 3: None}, 'c': {1: 0, 2: None, 3: None}, 'b': {1: 0, 2: None, 3: None}}
>>> import copy

As you can see, attempting to assign a specific member, d['a'][1] updates all values in d[*][1], not what I intended.

I thought the solution could be in using copy.deepcopy, but I might not be using it right:

>>> d2 = dict.fromkeys(r,copy.deepcopy(dict.fromkeys(c)))
>>> d2
{'a': {1: None, 2: None, 3: None}, 'c': {1: None, 2: None, 3: None}, 'b': {1: None, 2: None, 3: None}}
>>> d2['a'][1]=0
>>> d2
{'a': {1: 0, 2: None, 3: None}, 'c': {1: 0, 2: None, 3: None}, 'b': {1: 0, 2: None, 3: None}}

Any suggestions/pointers would be appreciated!

--
Pablo Englebienne
[EMAIL PROTECTED]






_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to