I would like to delete an element from a numpy array that has a certain value. However, in the case there are multiple elements of the same value, I only want to delete one occurrence (doesn't matter which one). That is:
import numpy as np
a = np.array([1, 1, 2, 6, 8, 8, 8, 9])
How do I delete one instance of 8? Specifically
a_new = np.delete(a, np.where(a == 8))
print(a_new)
removes all 8's.
You can simply choose one of the indices:
In [3]: np.delete(a, np.where(a == 8)[0][0])
Out[3]: array([1, 1, 2, 6, 8, 8, 9])
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