Python:在外循环中继续下一次迭代 [英] Python: Continuing to next iteration in outer loop

查看:37
本文介绍了Python:在外循环中继续下一次迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何内置方法可以在python的外循环中继续进行下一次迭代.例如,考虑以下代码:

I wanted to know if there are any built-in ways to continue to next iteration in outer loop in python. For example, consider the code:

for ii in range(200):
    for jj in range(200, 400):
        ...block0...
        if something:
            continue
    ...block1...

我希望这个 continue 语句退出 jj 循环并转到 ii 循环中的下一项.我可以用其他方式实现这个逻辑(通过设置一个标志变量),但是有没有一种简单的方法可以做到这一点,或者这是否要求太多?

I want this continue statement to exit the jj loop and goto next item in the ii loop. I can implement this logic in some other way (by setting a flag variable), but is there an easy way to do this, or is this like asking for too much?

推荐答案

for i in ...:
    for j in ...:
        for k in ...:
            if something:
                # continue loop i

在一般情况下,当您有多个循环级别并且 break 对您不起作用时(因为您想继续上面的循环之一,而不是当前循环正上方的循环),您可以执行以下操作之一

In a general case, when you have multiple levels of looping and break does not work for you (because you want to continue one of the upper loops, not the one right above the current one), you can do one of the following

def inner():
    for j in ...:
        for k in ...:
            if something:
                return


for i in ...:
    inner()

缺点是您可能需要向该新函数传递一些以前在作用域中的变量.您可以将它们作为参数传递,使它们成为对象上的实例变量(仅为此函数创建一个新对象,如果有意义的话),或者全局变量、单例,等等(ehm,ehm).

The disadvantage is that you may need to pass to that new function some variables, which were previously in scope. You can either just pass them as parameters, make them instance variables on an object (create a new object just for this function, if it makes sense), or global variables, singletons, whatever (ehm, ehm).

或者你可以将 inner 定义为一个嵌套函数,让它只捕获它需要的东西(可能会更慢?)

Or you can define inner as a nested function and let it just capture what it needs (may be slower?)

for i in ...:
    def inner():
        for j in ...:
            for k in ...:
                if something:
                    return
    inner()

使用例外

从哲学上讲,这就是异常的用途,在必要时通过结构化编程构建块(if、for、while)打破程序流.

Use exceptions

Philosophically, this is what exceptions are for, breaking the program flow through the structured programming building blocks (if, for, while) when necessary.

优点是您不必将一段代码分成多个部分.如果它是您在用 Python 编写时设计的某种计算,这很好.在这个早期引入抽象可能会减慢你的速度.

The advantage is that you don't have to break the single piece of code into multiple parts. This is good if it is some kind of computation that you are designing while writing it in Python. Introducing abstractions at this early point may slow you down.

这种方法的坏处是解释器/编译器作者通常假设异常是异常的,并相应地对其进行优化.

Bad thing with this approach is that interpreter/compiler authors usually assume that exceptions are exceptional and optimize for them accordingly.

class ContinueI(Exception):
    pass


continue_i = ContinueI()

for i in ...:
    try:
        for j in ...:
            for k in ...:
                if something:
                    raise continue_i
    except ContinueI:
        continue

为此创建一个特殊的异常类,这样您就不会冒意外使其他异常静音的风险.

Create a special exception class for this, so that you don't risk accidentally silencing some other exception.

我相信还有其他解决方案.

I am sure there are still other solutions.

这篇关于Python:在外循环中继续下一次迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆