如何使“for”循环中的索引向前跳N

ERJ*_*JAN 1 python indexing for-loop

我想要循环改变它的传递行为

例如,让它每隔 3 个索引跳到另外 2 个位置?

for i in range(n):
   if i % 3 == 0:
     print('found i div by 3, jump 2 positions forward')
     i = i + 2
Run Code Online (Sandbox Code Playgroud)

这不起作用。

它遍历i从 0 到n.

Shu*_*rma 5

您可以使用while循环,

i = 0
while i < n:
   if i % 3 == 0:
     print('found i div by 3, jump 2 positions forward')
     i = i + 2
     continue
   i = i + 1
Run Code Online (Sandbox Code Playgroud)