相关疑难解决方法(0)

打破嵌套循环

可能重复:
如何在Python中打破多个循环?

是否有一种更容易的方法来打破嵌套循环而不是抛出异常?(在Perl中,您可以为每个循环指定标签,并至少继续外循环.)

for x in range(10):
    for y in range(10):
        print x*y
        if x*y > 50:
            "break both loops"
Run Code Online (Sandbox Code Playgroud)

也就是说,有一个更好的方式:

class BreakIt(Exception): pass

try:
    for x in range(10):
        for y in range(10):
            print x*y
            if x*y > 50:
                raise BreakIt
except BreakIt:
    pass
Run Code Online (Sandbox Code Playgroud)

python loops

250
推荐指数
7
解决办法
31万
查看次数

标签 统计

loops ×1

python ×1