Here's the result of running your code in Ipython. Note that result
is a (2,0)
array, 2 rows, 0 columns, 0 elements. The append
produces a (2,)
array. result[0]
is (0,)
array. Your error message has to do with trying to assign that 2 item array into a size 0 slot. Since result
is dtype=float64
, only scalars can be assigned to its elements.
In [65]: result=np.asarray([np.asarray([]),np.asarray([])])
In [66]: result
Out[66]: array([], shape=(2, 0), dtype=float64)
In [67]: result[0]
Out[67]: array([], dtype=float64)
In [68]: np.append(result[0],[1,2])
Out[68]: array([ 1., 2.])
np.array
is not a Python list. All elements of an array are the same type (as specified by the dtype
). Notice also that result
is not an array of arrays.
Result could also have been built as
ll = [[],[]]
result = np.array(ll)
while
ll[0] = [1,2]
# ll = [[1,2],[]]
the same is not true for result.
np.zeros((2,0))
also produces your result
.
Actually there's another quirk to result
.
result[0] = 1
does not change the values of result
. It accepts the assignment, but since it has 0 columns, there is no place to put the 1
. This assignment would work in result was created as np.zeros((2,1))
. But that still can't accept a list.
But if result
has 2 columns, then you can assign a 2 element list to one of its rows.
result = np.zeros((2,2))
result[0] # == [0,0]
result[0] = [1,2]
What exactly do you want result
to look like after the append
operation?