How do I skip a few iterations in a for loop

后端 未结 4 1979
我寻月下人不归
我寻月下人不归 2021-02-05 03:55

In python I usually loop through ranges simply by

for i in range(100): 
    #do something

but now I want to skip a few steps in the loop. Mo

4条回答
  •  再見小時候
    2021-02-05 04:36

    Why not just set the value to skip until? Like:

    skip_until = 0
    for i in range(100):
        if i < skip_until:
            continue
        if SOME_CONDITION:
            skip_until = i + 10
        DO_SOMETHING()
    

    where SOME_CONDITION is whatever causes you to skip and DO_SOMETHING() is the actual loop contents?

提交回复
热议问题