I was wondering whether there is a more pythonic (and efficient) way of doing the following:
MAX_SIZE = 100
nbr_elements = 10000
y = np.random.randint(1, MAX_SIZE, nbr_elements)
REPLACE_EVERY_Nth = 100
REPLACE_WITH = 120
c = 0
for index, item in enumerate(y):
c += 1
if (c % REPLACE_EVERY_Nth == 0):
y[index] = REPLACE_WITH
So basically I generate a bunch of numbers from 1 to MAX_SIZE-1, and then I want to replace every REPLACE_EVERY_Nth element with REPLACE_WITH. This works fine but I guess it could be somehow done without using enumerate?
I was thinking something like this (which I know is wrong, because I replace the original y with the indices of y):
y = map(lambda x: REPLACE_WITH if not x%REPLACE_EVERY_Nth else x, range(len(y)))
is there a way to do modulo on the indices but replace the values?