Using Python3.5.3 in Spyder 3.1.3 on Raspbian on a Raspberry Pi. Appending two numpy-arrays to a list named 'list0' works well with allocated numpy arrays 'a' like:
import numpy as np
list0 = []
a = np.array([[1,2,3],[2,3,4]])
list0.append(a)
a = np.array([[11,12,13],[12,13,14]])
list0.append(a)
print("list0 =",list0)
works well, giving as output (a bit better formatted for the post):
list0 = [ array([[ 1, 2, 3], [ 2, 3, 4]]),
array([[11, 12, 13], [12, 13, 14]]) ]
Replacing the assignment to a using a loop, weird things happen:
import numpy as np
a = np.empty((3), int)
list0 = []
for idx in range(4):
for i in range(3):
a[i] = idx*10 + i
print("idx =",idx,"; a =",a)
list0.append(a)
print("list0 =",list0)
The second line tells Python the shape of the array used (in my original case it is a three-dimensional array). For verification the generated arrays named 'a' are printed out. Appending the newly filled arrays 'a' to 'list0' finally shows four times the last line.
idx = 0 ; a = [ 0 1 2]
idx = 1 ; a = [10 11 12]
idx = 2 ; a = [20 21 22]
idx = 3 ; a = [30 31 32]
list0 = [ array([30, 31, 32]), array([30, 31, 32]),
array([30, 31, 32]), array([30, 31, 32]) ]
I suppose that 'list0' simply contains four times a pointer to the array 'a' which only exists in one instance / memory range.
So: How can I physically append (copy?) each of the different arrays 'a' to the list? Is it a python bug or is it simply my misunderstanding of something? Certainly I should think more pythonian ;c)
Thanks for help, Peter