Indexing an array in numpy

Is there a numpy way to retrieve all the elements in an array except the element of the provided index.

x = array([[[4, 2, 3], [2, 0, 1], [1, 3, 4]], [[2, 1, 2], [3, 2, 3], [3, 4, 2]], [[2, 4, 1], [0, 2, 2], [4, 0, 0]]]) 

and asking

 x[not 1,:,:] 

You'll get

 array([[[4, 2, 3], [2, 0, 1], [1, 3, 4]], [[2, 4, 1], [0, 2, 2], [4, 0, 0]]]) 

thanks

+4
source share
3 answers
 In [42]: x[np.arange(x.shape[0])!=1,:,:] Out[42]: array([[[4, 2, 3], [2, 0, 1], [1, 3, 4]], [[2, 4, 1], [0, 2, 2], [4, 0, 0]]]) 
+6
source

Have you tried this?

 a[(0,2), :, :] 

Instead of blacklisting what you do not want to receive, you can try the whitelist that you need.

If you still need a blacklist, you can do something like this:

 a[[i for i in range(a.shape[0]) if i != 1], :, :] 

Basically, you just create a list with all possible indexes ( range(a.shape[0]) ) and filter out the ones you don’t want to display ( if i != 1 ).

+2
source

This is a pretty general solution:

 x[range(0,i)+range(i+1,x.shape[0]),:,:] 
+2
source

Source: https://habr.com/ru/post/1389069/


All Articles