简体   繁体   English

numpy中的数组索引

[英]Array indexing in numpy

Is there a way in numpy to retrieve all items in an array except the item of the index provided. numpy中是否有一种方法可以检索数组中除提供的索引项之外的所有项。

 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 by asking for 并要求

x[not 1,:,:] 

you will get 你会得到

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

   [[2, 4, 1],
    [0, 2, 2],
    [4, 0, 0]]])

Thanks 谢谢

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]]])

Have you tried this? 你试过这个吗?

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

Instead of blacklisting what you don't want to get, you can try to whitelist what you need. 您可以尝试将所需内容列入白名单,而不是将您不想要的内容列入黑名单。

If you need to blacklist anyway, 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 those that you don't want to get displayed ( if i != 1 ). 基本上你只是创建一个包含所有可能索引的列表( range(a.shape[0]) )并过滤掉那些你不想显示的列表( if i != 1 )。

这是一个非常通用的解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM