for
如果满足某个条件,是否可以阻止循环进入列表/迭代器中的下一个值?
lst = list('abcde')
for alphabet in lst:
if some_condition:
# somehow prevent the for loop from advancing so that in the
# next iteration, the value of alphabet remains the same as it is now
# do something
Run Code Online (Sandbox Code Playgroud)
你似乎想要的是一个嵌套while
循环.只有当while
循环退出时,循环才会for
继续下一个值.
alphabet = "abcdefghijklmnopqrstuvwxyz"
for letter in alphabet:
while some_condition:
# do something
Run Code Online (Sandbox Code Playgroud)