I'm trying to create a list of indices that cycles from 0
to m - 1
and is of length n
. I've thus far achieved this as follows:
import numpy as np
m = 7
n = 12
indices = [np.mod(i, m) for i in np.arange(n)]
which results in the following:
[0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4]
Is there a faster way to achieve this?
Thanks for any suggestions.
You can use islice
+ cycle
from itertools
:
from itertools import islice, cycle
print(list(islice(cycle(range(7)), 12)))
# [0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With