Numpy problem: Arrays in a list of dictionaries

Z

ZMY

I am new to Numpy/Pylab, and I am trying to construct a list of
dictionaries with arrays as the items, for example:
dict = {1: array([2, 3, 4]), 2: ''}
list1 = []
for i in range(3): list1.append(dict.copy()) ....
list1
[{1: array([2, 3, 4]), 2: ''}, {1: array([2, 3, 4]), 2: ''}, {1:
array([2, 3, 4]), 2: ''}]
[{1: array([ 2, 100, 4]), 2: ''}, {1: array([ 2, 100, 4]), 2:
''}, {1: array([ 2, 100, 4]), 2: ''}]
[{1: array([ 2, 100, 4]), 2: 'Jack'}, {1: array([ 2, 100, 4]),
2: ''}, {1: array([ 2, 100, 4]), 2: ''}]

So the strings can be assigned seperately but arrays can not. What is
the problem here?

I am using python2.4+Numpy1.0.1 on Fedora

Thanks a lot in advance.

- ZMY
 
R

Robert Kern

ZMY said:
I am new to Numpy/Pylab, and I am trying to construct a list of
dictionaries with arrays as the items, for example:
dict = {1: array([2, 3, 4]), 2: ''}
list1 = []
for i in range(3): list1.append(dict.copy()) ...
list1
[{1: array([2, 3, 4]), 2: ''}, {1: array([2, 3, 4]), 2: ''}, {1:
array([2, 3, 4]), 2: ''}]
list1[0][1][1]=100
list1
[{1: array([ 2, 100, 4]), 2: ''}, {1: array([ 2, 100, 4]), 2:
''}, {1: array([ 2, 100, 4]), 2: ''}]
list1[0][2]='Jack'
list1
[{1: array([ 2, 100, 4]), 2: 'Jack'}, {1: array([ 2, 100, 4]),
2: ''}, {1: array([ 2, 100, 4]), 2: ''}]

So the strings can be assigned seperately but arrays can not. What is
the problem here?

When you call dict.copy(), all it does is make a copy of the dictionary; each
copy of the dictionary still refers to the same objects.

list1[1] refers to the same array for all i, so when you modify it in-place
like you did, you will see the modifications in every reference to that object.

list[2] also referred to the same string for all i until you *replaced* the
entry in the 0'th dictionary.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
Z

ZMY

Dear Robert,

So how should I do this? I tried
d1 = {1: array([2, 3, 4]).copy(), 2: ''}
l1 = []
for i in range(3): l1.append(d1.copy())

and it still doesn't work.

I think my problem is: how to create a new array every time I append
it to the list...

Thanks in advance,

- ZMY


ZMYwrote:
I am new to Numpy/Pylab, and I am trying to construct a list of
dictionaries with arrays as the items, for example:
dict = {1: array([2, 3, 4]), 2: ''}
list1 = []
for i in range(3): list1.append(dict.copy()) ...
list1
[{1: array([2, 3, 4]), 2: ''}, {1: array([2, 3, 4]), 2: ''}, {1:
array([2, 3, 4]), 2: ''}]
list1[0][1][1]=100
list1
[{1: array([ 2, 100, 4]), 2: ''}, {1: array([ 2, 100, 4]), 2:
''}, {1: array([ 2, 100, 4]), 2: ''}]
list1[0][2]='Jack'
list1
[{1: array([ 2, 100, 4]), 2: 'Jack'}, {1: array([ 2, 100, 4]),
2: ''}, {1: array([ 2, 100, 4]), 2: ''}]
So the strings can be assigned seperately but arrays can not. What is
the problem here?

When you call dict.copy(), all it does is make a copy of the dictionary; each
copy of the dictionary still refers to the same objects.

list1[1] refers to the same array for all i, so when you modify it in-place
like you did, you will see the modifications in every reference to that object.

list[2] also referred to the same string for all i until you *replaced* the
entry in the 0'th dictionary.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
R

Robert Kern

ZMY said:
Dear Robert,

So how should I do this? I tried
d1 = {1: array([2, 3, 4]).copy(), 2: ''}
l1 = []
for i in range(3): l1.append(d1.copy())

and it still doesn't work.

Of course it doesn't, for the same reason that your first attempt didn't work
either. You've made a copy of an array and put the copy into the dictionary,
then made copies of that dictionary. The process of making copies of the
dictionary doesn't make copies of the objects it contains.
I think my problem is: how to create a new array every time I append
it to the list...

import copy

d1 = {1: array([2, 3, 4]), 2: ''}
l1 = []
for i in range(3):
l1.append(copy.deepcopy(d1))

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top