Skip to content Skip to sidebar Skip to footer

Creating Fixed Length Numpy Array From Variable Length Lists

I have been generating variable length lists, a simplified example. list_1 = [5] * 5 list_2 = [8] * 10 I then want to convert to np.array for manipulation. As such they need to b

Solution 1:

You did the job already. You may save some lines by using min function.

np_array = np.zeros(length)
l = min(length, len(list_like))
np_array[:l] = list_like[:l]

There is a bit more elegant way, by first creating an empty array, fill it up and then fill the tail with zeros.

Post a Comment for "Creating Fixed Length Numpy Array From Variable Length Lists"