deleting the indices in array

It’s better not iterating a container while changing its length. Try working with indexed instead :

import numpy as np
length=4
indices=np.arange(length)
for i in reversed(range(len(indices))):
        indices = np.delete(indices, i)
        print("checking", indices, i)

If you’re looking for deletion by element you could find the element index before using delete :

import numpy as np
length=4
elements=np.array([1, 3, 2, 4])
for i in elements:
        idx = np.where(elements == i)
        elements = np.delete(elements, idx)
        print("checking", elements, i)

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top