Create one-dimensional arrays of arrays in numpy

61 views Asked by At

As part of my current project I am using a third party package that at one point expects an array of shape (n,) which contains n arrays of shape (m_n,2). This array is created from a list of arrays using np.array(). Most of the time the subarrays can be expected not to have the same shape across all of them. This results in the desired output. Sometimes, however all subarrays have the same shape. Then np.array() returns an output of shape (n,m_n,2). Is there a way to force numpy to give me an output of shape (n,)? Alternatively, I'd be very thankful for a way to directly create an array like this.

Below is an example of my problem.

import numpy as np

a = np.zeros((3,2))
b = np.zeros((4,2))

list1 = [a, a]
list2 = [a, b]

array1 = np.array(list1)
array2 = np.array(list2)

print(np.shape(array1))
print(np.shape(array2))
0

There are 0 answers