Suppose I have a numpy array from which I want to remove a specific element.
# data = np.array([ 97 32 98 32 99 32 100 32 101])
# collect indices where the element locate
indices = np.where(data==32)
without_32 = np.delete(data, indices)
# without_32 become [ 97 98 99 100 101]
Now, suppose I want to restore the array (As I already have the indices where I should put the value 32
).
restore_data = np.insert(without_32, indices[0], 32)
But it gives IndexError: index 10 is out of bounds for axis 0 with size 9
. IS there other way to implement that?
update
It seems after delete the element I need some adjust for the indices like
restore_data = np.insert(without_32, indices[0]-np.arange(len(indices[0])), 32)
But Can I generalize this? Like not only 32
but also trace 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47
. I mean I want to trace the same way for 32-47
in a efficient way.