Python removes every nth element in an array

How to remove every nth element in an array?

import numpy as np x = np.array([0,10,27,35,44,32,56,35,87,22,47,17]) n = 3 # remove every 3rd element 

... something like the opposite of x[0::n] ? I tried this, but of course this does not work:

 for i in np.arange(0,len(x),n): x = np.delete(x,i) 
+8
python arrays numpy
source share
1 answer

You are close ... Pass the whole argea as a sublayer to remove, and not try to remove each element in turn, for example:

 import numpy as np x = np.array([0,10,27,35,44,32,56,35,87,22,47,17]) x = np.delete(x, np.arange(0, x.size, 3)) # [10 27 44 32 35 87 47 17] 
+9
source share

All Articles