I want to create a Numpy array, which contains two (Python-) lists. When i try to append an element to one of those lists, the code crashes.
import numpy as np
list0 = [0,0,0]
list1 = [1,1,1]
#list1.append(0)
print(type(list0))
print(type(list1))
array0 = np.array((list0, list1))
array0[0].append(42)
print(array0)
The confusing thing is that when i uncomment the 4th line, the code works just fine.
The error message i get:
File "test.py", line 10, in <module>
array0[0].append(3)
AttributeError: 'numpy.ndarray' object has no attribute 'append'
I run on python 3.5.1 and numpy 1.10.4
np.array(((0,0,0),(1,1,1)))tries to create a 2d array. This does not happen, if the two lists (e.g.(0,0,0)and(1,1,1)) have different size (e.g.(0,0)and(1,1,1)). A way to initialize an array with two empty lists is to writearray0 = np.empty(2, dtype=np.object)array0[:] = [], []np.arraydefaults to making a mulidimensional array. Making an object dtype is the 2nd class backup choice. It may be better to use plain lists, even faster.