I have lists of different size like
a = [1,2,3]
b = [4,5]
and i want to create a two dimensional array with these values and a default value at places where the short lists have no data available.
c = do_something_with_a_b()
In: c
Out: array([[1,2,3],
[4,5, DEFAULT_VALUE]])
At the moment i use the following but i think this is over complicated:
all_ar = []
all_ar.append(a)
all_ar.append(b)
# Get the size of all arrays for masking
len_ar = np.array([array.size for array in all_ar])
# Create a mask according to the length of the arrays
mask = np.arange(len_ar.max()) < len_ar[:,None]
# Create an array filled with the default value, here -1
c = np.full(mask.shape, -1, dtype='int')
# Use the mask to overwrite the the default values with the
# data from the arrays
c[mask] = np.concatenate(all_ar)
In: c
Out: array([[1,2,3],
[4,5,-1]])
Is there an easier way to convert irregular sized lists to a numpy array with regular shape and a default value at missing data points ?