Reputation: 1133
I have a list of python arrays like the following:
[array([[0., 0., 0.]]),
array([[0., 0., 0.]]),
array([[0., 0., 0.]])]
My goal is to change them to an array of lists like the following:
array([[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0]])
I tried the following however I'm getting I'm an error:
np.array([my_array[i].tolist() for i in my_array])
The following is the error:
TypeError: only integer scalar arrays can be converted to a scalar index
Could anyone help me understand what's going and what I'm doing wrong.
Upvotes: 0
Views: 81
Reputation: 419
Try:
np.array([i[0] for i in my_array])
Since in your case, i is not the index of a, it represents each element in my_array
And each of your element in my_array
is a nested list, you may want to flat the nested list when doing the conversion
Upvotes: 0
Reputation: 7210
You can just concatenate row-wise with vstack
since they are of shape (1, 3)
in your example to get the result of shape (n, 3)
.
np.vstack(my_array)
Why your current code fails is because you are iterating over every sub-array in my_array
and trying to index my_array
with that. So you are indexing a list with array([[0., 0., 0.]])
which is not a scaler index because it is an array.
np.array(my_array).reshape(-1, 3)
np.array([e.reshape(-1) for e in my_array])
...
Upvotes: 1