如何选择NumPy数组中除索引序列以外的所有元素 [英] How to select all elements in a NumPy array except for a sequence of indices

查看:453
本文介绍了如何选择NumPy数组中除索引序列以外的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一些长数组和一个索引列表.我该如何选择除那些索引以外的所有内容?我找到了一个解决方案,但它并不优雅:

Say I have some long array and a list of indices. How can I select everything except those indices? I found a solution but it is not elegant:

import numpy as np
x = np.array([0,10,20,30,40,50,60])
exclude = [1, 3, 5]
print x[list(set(range(len(x))) - set(exclude))]

推荐答案

这是

This is what numpy.delete does. (It doesn't modify the input array, so you don't have to worry about that.)

In [4]: np.delete(x, exclude)
Out[4]: array([ 0, 20, 40, 60])

这篇关于如何选择NumPy数组中除索引序列以外的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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