如何防止for循环在Python中推进?

Man*_*anu 2 python for-loop

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)

kin*_*all 8

你似乎想要的是一个嵌套while循环.只有当while循环退出时,循环才会for继续下一个值.

alphabet = "abcdefghijklmnopqrstuvwxyz"
for letter in alphabet:
  while some_condition:
     # do something
Run Code Online (Sandbox Code Playgroud)