Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NumPy 1D array slicing

I have a NumPy array like:

a = np.array([1,2,3,4,0,0,5,6,7,8,0,0,9,10,11,12])

What is the most effective way to select all values except values (in my example is 0) at some positions?

So I need to get an array:

[1,2,3,4,5,6,7,8,9,10,11,12]

I know how to skip the one nth value with [::n] construction but is it possible to skip several values using the similar syntax?

Thank you for any help!

like image 713
Vitali Molchan Avatar asked Feb 11 '23 11:02

Vitali Molchan


1 Answers

You probably want np.delete:

>>> np.delete(a, [4, 5, 10, 11])
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12])
like image 182
Jaime Avatar answered Feb 16 '23 02:02

Jaime