Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delete the different rows in array with a loop

I have a numpy array with multiple rows, I want to delete the rows with index 0, 1, 2 and 6 +0, 6+1, 6+2, and 2 * 6+0, 2 * 6+1, and 2 * 6+2, and … c * 6+0, c * 6+1, c * 6+2. I know that is possible to use the np.delete, however I don’t know how to loop over the different indices. Here is an example:

a = np.array([[4, 5],
   [4, 2],
   [1, 2],
   [2, 3],
   [3, 1],
   [0, 1],
   [1, 1],
   [1, 0],
   [1, 5],
   [5, 4],
   [2, 3],
   [5, 5]])

The output which I want is :

out = np.array([
   [2, 3],
   [3, 1],
   [0, 1],
   [5, 4],
   [2, 3],
   [5, 5]])

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Instead of deleting, you could filter them out:

out = a[~np.isin(np.arange(len(a))%6, [0,1,2])]

Output:

array([[2, 3],
       [3, 1],
       [0, 1],
       [5, 4],
       [2, 3],
       [5, 5]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading