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
.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With