0

How do I supplemented by a numpy array of zeros?

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

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

For example size = 5;

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

Should I call fill or make a new array like arr + [0]*(size-len)?

Which is faster?

1 Answer 1

1

I'd use ndarray.resize:

>>> a = np.array([1,2,3])
>>> a.resize((5,))
>>> a
array([1, 2, 3, 0, 0])
Sign up to request clarification or add additional context in comments.

2 Comments

@JulienBernu -- Yeah, I just looked for a np.resize -- It turns out that there is np.resize and also ndarray.resize. The former repeats the array which isn't what OP wants, but the latter pads with 0 as OP wants, so I don't see any good reason not to use it... :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.