Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete an object from a numpy array without knowing the index

Tags:

python

list

numpy

Is it possible to delete an object from a numpy array without knowing the index of the object but instead knowing the object itself?

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.

Example:

[a,b,c,d,e,f]

x = e

I would like to delete x.

like image 313
MhmdMnsr Avatar asked Apr 01 '16 21:04

MhmdMnsr


People also ask

How do I remove an object from a NumPy array?

To remove an element from a NumPy array: Specify the index of the element to remove. Call the numpy. delete() function on the array for the given index.

How do I remove a value from an array in Python?

Removing Python Array Elements We can delete one or more items from an array using Python's del statement. We can use the remove() method to remove the given item, and pop() method to remove an item at the given index.


1 Answers

You can find the index/indices of the object using np.argwhere, and then delete the object(s) using np.delete.

Example:

x = np.array([1,2,3,4,5]) index = np.argwhere(x==3) y = np.delete(x, index) print(x, y) 
like image 129
jojonas Avatar answered Oct 17 '22 02:10

jojonas