I have an array that was created from lists of varying lengths. I do not know the length of the lists beforehand which is why I was using lists instead of arrays.
Here's a reproducible code for the purpose of this question:
a = []
for i in np.arange(5):
a += [np.random.rand(np.random.randint(1,6))]
a = np.array(a)
Is there a more efficient way, than the following, to convert this array into a well structured array with the rows being the same size with NaNs?
max_len_of_array = 0
for aa in a:
len_of_array = aa.shape[0]
if len_of_array > max_len_of_array:
max_len_of_array = len_of_array
max_len_of_array
n = a.shape[0]
A = np.zeros((n, max_len_of_array)) * np.nan
for i, aa in enumerate(zip(a)):
A[i][:aa[0].shape[0]] = aa[0]
A