从python中循环内部的循环中跳过一个父循环

时间:2011-07-23 14:30:52

标签: python loops

  

可能重复:
  Python: Continuing to next iteration in outer loop

也许标题有点令人困惑,但你会理解我在代码中的含义:

for item in items: #i want to skip one loop of this bucle
    for i in item: #loop nº2
        if i==5:
            continue #but this only skip a loop in nº2, there's no propagation

for item in items: #i want to skip one loop of this bucle for i in item: #loop nº2 if i==5: continue #but this only skip a loop in nº2, there's no propagation 我怎样才能使这个工作? 提前谢谢。

1 个答案:

答案 0 :(得分:4)

设置一个标志,突破内循环,检查主循环中的标志,然后继续。

  for i = 1 to N do
     flag = false
     for j = 1 to M do
        ...
        if condition then
           flag = true
           break
        ...
     if flag then continue
     ...