使用索引数组获取元素的更好方法? [英] better way to use index array to get elements?

查看:90
本文介绍了使用索引数组获取元素的更好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用像这样的索引数组来获取numpy数组的元素

I want to get the elements of a numpy array using an index array like so

import numpy 
a = numpy.arange(6)
ind = [2,3]

现在,a[ind]给了我第3个和第4个元素,但实际上我想要a的所有 other 个元素. 有没有一种方法可以做到这一点?

now, a[ind] gives me the 3rd and 4th element, but I actually want all the other elements of a. Is there a one line/ elegant way to do this?

推荐答案

我不知道要获得一组整数索引的补码的直接方法.布尔索引取反很容易,它使您可以执行以下操作:

There isn't a straightforward way I know of to get the complement of a set of integer indices. Boolean index negation is easy, which lets you do something like this:

In [100]: a=np.arange(6)

In [101]: ind=[2,3]

In [102]: cind=(a==a)

In [103]: cind[ind]=False

In [104]: a[cind]
Out[104]: array([0, 1, 4, 5])

但这不是单行解决方案.

But it isn't a one line solution.

这篇关于使用索引数组获取元素的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆