weider weider - 6 days ago 7
Python Question

supplement numpy array in python 3

How to supplemented by an numpy array of zeros?

arr=np.array([1,10])

if len(arr)<size:
*supplemented array of zeros to the size*.


For example sie =5;

if array = [1,2,3].
output array = [1,2,3,0,0].


Should I use fill or make new array like
arr+[0]*(size-len)
?
Which way is faster?

Many thanks

Answer

I'd use ndarray.resize:

>>> a = np.array([1,2,3])
>>> a.resize((5,))
>>> a
array([1, 2, 3, 0, 0])
Comments