打破 Python 中的嵌套(双)循环

发布于 2024-08-28 18:56:33 字数 274 浏览 4 评论 0原文

我使用以下方法来打破Python中的双循环。

for word1 in buf1:
    find = False
    for word2 in buf2:
        ...
        if res == res1:
            print "BINGO " + word1 + ":" + word2
            find = True
    if find:
        break

有没有更好的方法来打破双循环?

I use the following method to break the double loop in Python.

for word1 in buf1:
    find = False
    for word2 in buf2:
        ...
        if res == res1:
            print "BINGO " + word1 + ":" + word2
            find = True
    if find:
        break

Is there a better way to break the double loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烧了回忆取暖 2024-09-04 18:56:33

Python 中打破嵌套循环的推荐方法是... Exception

class Found(Exception): pass
try:
    for i in range(100):
        for j in range(1000):
            for k in range(10000):
               if i + j + k == 777:
                  raise Found
except Found:
    print i, j, k 

The recommended way in Python for breaking nested loops is... Exception

class Found(Exception): pass
try:
    for i in range(100):
        for j in range(1000):
            for k in range(10000):
               if i + j + k == 777:
                  raise Found
except Found:
    print i, j, k 
对你的占有欲 2024-09-04 18:56:33

可能不是您所希望的,但通常您希望在将 find 设置为 True 后进行 break

for word1 in buf1: 
    find = False 
    for word2 in buf2: 
        ... 
        if res == res1: 
            print "BINGO " + word1 + ":" + word2 
            find = True 
            break             # <-- break here too
    if find: 
        break 

另一种方法是使用生成器表达式将 for 压缩为单个循环

for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2):
    ... 
    if res == res1: 
        print "BINGO " + word1 + ":" + word2
        break 

您也可以考虑使用 itertools.product

from itertools import product
for word1, word2 in product(buf1, buf2):
    ... 
    if res == res1: 
        print "BINGO " + word1 + ":" + word2
        break 

Probably not what you are hoping for, but usually you would want to have a break after setting find to True

for word1 in buf1: 
    find = False 
    for word2 in buf2: 
        ... 
        if res == res1: 
            print "BINGO " + word1 + ":" + word2 
            find = True 
            break             # <-- break here too
    if find: 
        break 

Another way is to use a generator expression to squash the for into a single loop

for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2):
    ... 
    if res == res1: 
        print "BINGO " + word1 + ":" + word2
        break 

You may also consider using itertools.product

from itertools import product
for word1, word2 in product(buf1, buf2):
    ... 
    if res == res1: 
        print "BINGO " + word1 + ":" + word2
        break 
初见终念 2024-09-04 18:56:33

使用函数进行重构,以便您可以在找到“宾果游戏”时返回。

允许显式打破嵌套循环的提议已被拒绝:
http://www.python.org/dev/peps/pep-3136/

Refactor using functions so you can return when you find your "bingo".

The proposal to allow explicit breaking out of nested loops has been rejected:
http://www.python.org/dev/peps/pep-3136/

时间海 2024-09-04 18:56:33

大多数时候,您可以使用多种方法来创建一个与双循环执行相同操作的单循环。

在您的示例中,您可以使用 itertools.product 替换代码片段其他

import itertools
for word1, word2 in itertools.product(buf1, buf2):
    if word1 == word2:
        print "BINGO " + word1 + ":" + word2
        break

itertools 函数也适用于其他模式。

Most times you can use a number of methods to make a single loop that does the same thing as a double loop.

In your example, you can use itertools.product to replace your code snippet with

import itertools
for word1, word2 in itertools.product(buf1, buf2):
    if word1 == word2:
        print "BINGO " + word1 + ":" + word2
        break

The other itertools functions are good for other patterns too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文