0
$\begingroup$

I have an array of length L with N zeros, and L-N non-zero values. I have another array of length N. I would like to put the values of the shorter array into the positions of the longer array which are zero, keeping them in order. For example, if I had,

$$ L = \, [1,2,3,0,0,4,5,6,0] $$ $$ N = \, [10,0,20] $$ I would want, $$ L' = \, [1,2,3,10,0,4,5,6,20] $$ I will do this repeatedly with the same $L$ and many different $N$ (all of the same length). What is the best way to accomplish this using Numpy?

$\endgroup$

closed as off-topic by Bill Greene, Christian Clason, Kirill, nicoguaro Dec 4 '18 at 23:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1
$\begingroup$
L[L==0]=N

Unless I’m missing something in your question. In numpy the main constraint is that you want to work with built-in array objects as much as possible. But taking a subset of an array, with indices given by a logical array, is a built-in operation so I expect this to be sufficient. This modifies L in-place, so you’ll need to create a copy for every new N.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.