I have the following code.
for k in range( ( N + 1 ) * K ):
if k >= 0 and k <= K-1:
# do something
# n = 0
elif k >= K and k <= 2*K-1:
# do something
# n = 1
elif k >= 2*K and k <= 3*K-1:
# do something
# n = 2
...
...
The 'do something' is complicated to explain but I replaced it with the affectation n = p.
How can I write this explicitly?
More specifically, if k is in the set {p*K,...,(p+1)*K-1} for p = 0 to N, then do something. How can I do it in a code?
You could just have three loops, no?
for k in range(K):
# do something
for k in range(K, 2*K-1):
# do something
for k in range(2*K-1, (N+1)*K):
# do the rest
for loop_id in xrange(N):
for i in xrange(K):
k = K * loop_id + i
do_smth(loop_id, i, k)
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