Python 如何在不知道索引的情况下从 numpy 数组中删除对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36365990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete an object from a numpy array without knowing the index
提问by MhmdMnsr
is it possible to delete an object from a numpy array without knowing the index of the object but instead knowing the object itself?
是否可以在不知道对象的索引但知道对象本身的情况下从 numpy 数组中删除对象?
i have seen that it is possible using the index of the object using the np.delete function, but i'm looking for a way to do it having the object but not its index
我已经看到可以使用 np.delete 函数使用对象的索引,但我正在寻找一种方法来实现它的对象而不是它的索引
example:
例子:
[a,b,c,d,e,f]
x = e
[a,b,c,d,e,f]
x = e
I would like to delete x
我想删除 x
thx in advance
提前谢谢
回答by jojonas
You can find the index/indices of the object using np.argwhere, and then delete the object(s) using np.delete.
你可以找到索引/使用对象的指数np.argwhere,然后删除使用对象(或多个)np.delete。
Example:
例子:
x = np.array([1,2,3,4,5])
index = np.argwhere(x==3)
y = np.delete(x, index)
print(x, y)
回答by Ulf Aslak
Cast it as a numpy array, and mask it out:
将其转换为一个 numpy 数组,并将其屏蔽:
x = np.array(list("abcdef"))
x = x[x!='e'] # <-- THIS IS THE METHOD
print x
# array(['a', 'b', 'c', 'd', 'f'])
Doesn't have to be more complicated than this.
不必比这更复杂。
回答by hpaulj
Boolean indexing or masking is a good basic way of selecting, or removing specific elements of an array
布尔索引或屏蔽是选择或删除数组特定元素的一种很好的基本方法
You talk about removing a specific 'object'. Let's take that literally and define an array of dtype object:
您谈论删除特定的“对象”。让我们从字面上理解并定义一个 dtype 对象数组:
In [2]: x=np.array(['a','b','c','d','e'],dtype=object)
In [3]: x
Out[3]: array(['a', 'b', 'c', 'd', 'e'], dtype=object)
In [4]: x=='d' # elements that equal 'd'
Out[4]: array([False, False, False, True, False], dtype=bool)
In [5]: x!='d' # elements that don't
Out[5]: array([ True, True, True, False, True], dtype=bool)
In [6]: x[x!='d'] # select a subset
Out[6]: array(['a', 'b', 'c', 'e'], dtype=object)
Behind the scenes argwhere
and delete
use this. Note that argwhere
uses the x==d
boolean array, converting it to array indices. And constructing mask like this is one way that delete
operates.
在幕后argwhere
,并delete
使用它。请注意,argwhere
使用x==d
布尔数组,将其转换为数组索引。像这样构建掩码是一种delete
操作方式。
There are some important limits:
有一些重要的限制:
that equality (or not equality) test has to work for your values. It might not if the elements are floats.
deleting from a 1d array is easier than from a 2d (or larger) one. With 2d you have to decide whether to delete a row, a column, or an element (and in the process flattening the array).
deleting only one element of that matches is a bit trickier.
相等(或不相等)测试必须适用于您的价值观。如果元素是浮点数,则可能不会。
从一维数组中删除比从二维(或更大)数组中删除更容易。使用 2d,您必须决定是删除行、列还是元素(并在此过程中将数组展平)。
只删除匹配的一个元素有点棘手。
For some cases it might be better to .tolist()
the array and use a list method.
在某些情况下.tolist()
,使用数组并使用列表方法可能会更好。
In [32]: xl=x.tolist()
In [33]: xl.remove('d')
In [34]: np.array(xl,dtype=object)
Out[34]: array(['a', 'b', 'c', 'e'], dtype=object)
There's no exact equivalent to list.remove
for arrays.
没有完全等同于list.remove
for 数组。
回答by Shoresh
arr = np.array(['a','b','c','d','e','f'])
arr = np.array(['a','b','c','d','e','f'])
Then
然后
arr = [x for x in arr if arr != 'e']
arr = [x for x in arr if arr != 'e']