【问题标题】:Can you "restart" the current iteration of a Python loop?你能“重启”一个 Python 循环的当前迭代吗?
【发布时间】:2015-06-28 21:05:22
【问题描述】:

有没有办法实现这样的东西:

for row in rows:
    try:
        something
    except:
        restart iteration

【问题讨论】:

  • 您可以跳到下一个迭代,但不能重试当前迭代。
  • 是的,它被称为内循环。
  • 你的意思是像 perl 中的redo?我认为python中没有这样的东西。
  • 我认为他们应该在程序语言中添加在同一迭代中返回循环开始的能力(就像他们做了 continue 和 break,他们应该开发“reiterate”

标签: python for-loop iteration


【解决方案1】:

我的 2¢,如果 rows 是一个列表,你可以这样做

for row in rows:
    try:
        something
    except:
        # Let's restart the current iteration
        rows.insert(rows.index(row), row)
        continue  # <-- will skip `something_else`, i.e will restart the loop.

    something_else
    

此外,在其他条件相同的情况下,for 循环比 Python 中的 while 循环更快。 话虽如此,性能并不是 Python 中的首要决定因素。

【讨论】:

    【解决方案2】:

    试试这个

    it = iter(rows)
    while True:
        try:
            something
            row = next(it)        
        except StopIteration:
            it = iter(rows)
    

    【讨论】:

      【解决方案3】:

      您可以将行设置为迭代器,并且仅在没有错误时前进。

      it = iter(rows)  
      row = next(it,"")
      while row:
          try:
              something
              row = next(it,"")
          except:
             continue
      

      附带说明,如果您还没有,我会在异常中捕获特定的错误/错误,您不想捕获所有内容。

      如果你有 Falsey 值,你可以使用 object 作为默认值:

      it = iter(rows)
      row, at_end = next(it,""), object()
      while row is not at_end:
          try:
              something
              row = next(it, at_end)
          except:
              continue
      

      【讨论】:

      • 这对我来说似乎比while True: 的答案更清楚
      【解决方案4】:

      让你的 for 循环在一个无限的 while 循环中。使用 if else 条件检查要重新启动 for 循环的条件并中断内部循环。在 while 循环内有一个 if 条件,它在 for 循环之外以中断 while 循环。 像这样:

          while True:
            for row in rows:
              if(condition)
              .....
              if(condition)
               break
         if(condition)
           break
      

      【讨论】:

        【解决方案5】:

        虽然我不建议这样做,但唯一的方法是创建一个 While (True) 循环,直到它得到 something Done。

        记住无限循环的可能性。

        for row in rows:
            try:
                something
            except:
                flag = False
                while not flag:
                    try: 
                       something
                       flag = True
                    except:
                       pass
        

        【讨论】:

          【解决方案6】:

          您可以将您的 try/except 块放在另一个循环中,然后在它成功时中断:

          for row in rows:
              while True:
                  try:
                      something
                      break
                  except Exception: # Try to catch something more specific
                      pass
          

          【讨论】:

          • “pass”不会导致for循环继续吗?
          • 不,因为它在 while 循环内。代码将刚好落在except 块的末尾并循环回到while 循环的顶部。请注意,如果您愿意,也可以使用continue
          • 所以我会把它放在我的 for 循环的实际内容之前,它只是运行直到“某事”被确认通过,然后它在我的 for 循环中运行实际的“某事”?
          • 是的,这是正确的。在上面的示例中,您可以将something 替换为您要确认通过的代码。其余代码将进入 while 循环,直到 while 循环退出。
          • 但这只会永远循环while,但不会重新启动for循环。我认为问题是是否可以重复 for 循环。
          猜你喜欢
          • 2020-01-07
          • 1970-01-01
          • 1970-01-01
          • 2016-08-12
          • 2021-11-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-12
          相关资源
          最近更新 更多