<legend id='fAakf'><style id='fAakf'><dir id='fAakf'><q id='fAakf'></q></dir></style></legend>
      <i id='fAakf'><tr id='fAakf'><dt id='fAakf'><q id='fAakf'><span id='fAakf'><b id='fAakf'><form id='fAakf'><ins id='fAakf'></ins><ul id='fAakf'></ul><sub id='fAakf'></sub></form><legend id='fAakf'></legend><bdo id='fAakf'><pre id='fAakf'><center id='fAakf'></center></pre></bdo></b><th id='fAakf'></th></span></q></dt></tr></i><div id='fAakf'><tfoot id='fAakf'></tfoot><dl id='fAakf'><fieldset id='fAakf'></fieldset></dl></div>
      • <bdo id='fAakf'></bdo><ul id='fAakf'></ul>

        <small id='fAakf'></small><noframes id='fAakf'>

      1. <tfoot id='fAakf'></tfoot>

        在 Python 3 中不使用“break"就停止迭代

        Stopping an iteration without using `break` in Python 3(在 Python 3 中不使用“break就停止迭代)
          <tbody id='SvAEd'></tbody>
          <legend id='SvAEd'><style id='SvAEd'><dir id='SvAEd'><q id='SvAEd'></q></dir></style></legend>
          <i id='SvAEd'><tr id='SvAEd'><dt id='SvAEd'><q id='SvAEd'><span id='SvAEd'><b id='SvAEd'><form id='SvAEd'><ins id='SvAEd'></ins><ul id='SvAEd'></ul><sub id='SvAEd'></sub></form><legend id='SvAEd'></legend><bdo id='SvAEd'><pre id='SvAEd'><center id='SvAEd'></center></pre></bdo></b><th id='SvAEd'></th></span></q></dt></tr></i><div id='SvAEd'><tfoot id='SvAEd'></tfoot><dl id='SvAEd'><fieldset id='SvAEd'></fieldset></dl></div>
        1. <small id='SvAEd'></small><noframes id='SvAEd'>

          <tfoot id='SvAEd'></tfoot>
              • <bdo id='SvAEd'></bdo><ul id='SvAEd'></ul>
                  本文介绍了在 Python 3 中不使用“break"就停止迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  例如,这段代码可以在没有break(并且没有continuereturn)的情况下重写吗?

                  For example, can this code be rewritten without break (and without continue or return)?

                  import logging
                  
                  for i, x in enumerate(x):
                      logging.info("Processing `x` n.%s...", i)
                      y = do_something(x)
                      if y == A:
                          logging.info("Doing something else...")
                          do_something_else(x)
                      elif y == B:
                          logging.info("Done.")
                          break
                  

                  由于有些人批评在循环中使用 breakcontinue,我想知道 Python 是否允许编写 for 循环而不使用他们.我会说 Python 不允许这样做(也许它会违反一种方法"的规则).

                  Since some people criticize the use of break and continue inside loops, I was wondering whether Python allowed to write for loops without them. I would say that Python doesn't allow this (and maybe it would go against the "one way to do it" rule).

                  评论者让我注意到可以使用 return 代替,但这也不是解决方案.

                  Commenters have made me notice that return could be used instead, but that would not be a solution, either.

                  推荐答案

                  你总是可以使用一个函数并从它返回:

                  You could always use a function and return from it:

                  import logging
                  
                  def func():
                      for i, x in enumerate(x):
                          logging.info("Processing `x` n.%s...", i)
                          y = do_something(x)
                          if y == A:
                              logging.info("Doing something else...")
                              do_something_else(x)
                          elif y == B:
                              logging.info("Done.")
                              return # Exit the function and stop the loop in the process.
                  func()
                  

                  虽然在我看来使用 break 更优雅,因为它让你的意图更清晰.

                  Although using break is more elegant in my opinion because it makes your intent clearer.

                  这篇关于在 Python 3 中不使用“break"就停止迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                1. <i id='FWGsk'><tr id='FWGsk'><dt id='FWGsk'><q id='FWGsk'><span id='FWGsk'><b id='FWGsk'><form id='FWGsk'><ins id='FWGsk'></ins><ul id='FWGsk'></ul><sub id='FWGsk'></sub></form><legend id='FWGsk'></legend><bdo id='FWGsk'><pre id='FWGsk'><center id='FWGsk'></center></pre></bdo></b><th id='FWGsk'></th></span></q></dt></tr></i><div id='FWGsk'><tfoot id='FWGsk'></tfoot><dl id='FWGsk'><fieldset id='FWGsk'></fieldset></dl></div>
                    • <bdo id='FWGsk'></bdo><ul id='FWGsk'></ul>

                      <small id='FWGsk'></small><noframes id='FWGsk'>

                        <tbody id='FWGsk'></tbody>
                      <tfoot id='FWGsk'></tfoot>
                      <legend id='FWGsk'><style id='FWGsk'><dir id='FWGsk'><q id='FWGsk'></q></dir></style></legend>