我有以下代码.
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 ... ...
“做某事”很难解释,但我用矫揉造作n = p取而代之.
我怎么能明确写出来?
更具体地说,如果对于p = 0到N,k在集合{p * K,…,(p 1)* K-1}中,那么做一些事情.我怎么能在代码中做到这一点?
解决方法
for loop_id in xrange(N): for i in xrange(K): k = K * loop_id + i do_smth(loop_id,i,k)