Home  >  Article  >  Backend Development  >  Taking stock of ideas for interrupting multiple loops in Python

Taking stock of ideas for interrupting multiple loops in Python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼forward
2019-09-26 17:37:092061browse

Taking stock of ideas for interrupting multiple loops in Python

I. Breaking out of a single loop

No matter what the programming language is, there may be a need to jump out of the loop, such as when enumerating, It terminates when a number that satisfies the condition is found. Breaking out of a single loop is very simple, such as:

for i in range(10):
    if i > 5:
        print i
        break

However, sometimes we need to jump out of multiple loops, and break can only jump out of one loop, such as:

for i in range(10):
    for j in range(10):
        if i+j > 5:
            print i,j
            break

Such code is not It is said that it stops when a group of i j > 5 is found, but 10 groups are found continuously, because break only jumps out of the for j in range (10) loop. So, how can we jump out of the multi-weighted category? Make a note here.

Related recommendations: "Python Tutorial"

II. Breaking out of multiple loops

In fact, the standard syntax of Python is It does not support jumping out of multiple loops, so you can only use some techniques. The general ideas are: writing as a function, using Cartesian product, and using debugging.

Of course the most common idea is to use variable notation

def f():
    flag = 0
    for i in range(10):
        for j in range(i):
            if i+j>5:
                print i,j
                flag = 1
                break
        if flag == 1:
            break
if __name__ == "__main__":
    f()

Written as a function

In Python, the function will stop when it reaches the return statement , so you can use this feature to write functions as functions to terminate multiple loops.

For example:

def work():
    for i in range(10):
        for j in range(10):
            if i+j > 5:
                return i,j
print work()

Using Cartesian product

The idea of ​​​​this method is that since I can jump out of a single loop, I will rewrite the multiple loops For a single loop, you can use the Cartesian product function product in itertools, for example:

from itertools import product
for i,j in product(range(10), range(10)):
    if i+j > 5:
        print i,j
        break

Use the debug mode

The Cartesian product method is very clever and very Simple, but it can only be used when the collections of each loop are independent. If each level of loop is closely related to the previous level, this technique cannot be used. At this time, you can use the first method and write it as a function. In addition, you can also use debugging mode. This takes advantage of the principle of exiting as soon as an error occurs in debugging mode. It disguises an error.

class Found(Exception):
    pass
try:
    for i in range(10):
        for j in range(i): #第二重循环跟第一重有关
            if i + j > 5:
                raise Found
except Found:
    print i, j

The above is the detailed content of Taking stock of ideas for interrupting multiple loops in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete