在python中中断嵌套循环,中断所有循环

在python中中断嵌套循环,中断所有循环,python,loops,for-loop,break,Python,Loops,For Loop,Break,我有两个嵌套循环 我想要的是:当条件满足时退出内部循环,然后继续外部循环 问题:“break”命令中断所有循环 代码如下: for path in paths: for edge in path: ... #some stuff here ... if condition==TRUE: break # i want exit from the inner loop and continue the external

我有两个嵌套循环

我想要的是:当条件满足时退出内部循环,然后继续外部循环

问题:“break”命令中断所有循环

代码如下:

for path in paths:
    for edge in path:
       ...
       #some stuff here
       ...
       if condition==TRUE:
         break # i want exit from the inner loop and continue the external loop whit next path in paths list
上面的代码不起作用,因为break命令会打断所有循环,但我想停止内部循环,并继续外部循环到列表路径中的下一个元素路径

谢谢你的帮助

编辑:这是我的完整功能:命令break停止两个循环。为什么?

def feasible_path(G,paths):
#G is a graph
#paths is a list of all path in G between two nodes
paths_to_remove=[]
lenght_paths=len(paths)

for i in range(0,lenght_paths-1,1):
    minCap=sys.maxint
    print 'selected:'
    print paths[i]
    index=0
    lenght_path=len(paths[i])

    for index in range(0,(lenght_path)-1,1):
        id_source=(paths[i])[index]
        id_target=(paths[i])[index+1]

        if G.has_edge(id_source,id_target,key=0):
            cap_edge=G[id_source][id_target][0]['capacity']
            if(cap_edge<minCap):
                minCap=cap_edge

        else: # edge mising
            paths_to_remove.append(paths[i])
            minCap=-1
            break   #i want stop the for index loop

    if(minCap != -1):
        index=0
        for index in range(0,len(paths[i])-1,1):
            id_source=(paths[i])[index]
            id_target=(paths[i])[index+1]
            if G.has_edge(id_source,id_target,key=0):
                old_capacity=G[id_source][id_target][0]['capacity']
                new_capacity=old_capacity-minCap
                G[id_source][id_target][0]['capacity']=new_capacity
                if(new_capacity==0):
                    G.remove_edge(id_source,id_target,key=0)


print 'path to remove'
print paths_to_remove
return paths
输出:

[1, 2, 3]
    1
    2
    3
break
[3, 4, 5]
    3
break
两次中断:因此中断仅停止内部循环

编辑: 使用您的代码和足够测试代码的最小图形…:

import sys
class Graph():
    def __init__(self):
        self.edges = set()

    def has_edge(self, src, trgt, key):
        return src+trgt in self.edges

    def remove_edge(self, src, trgt):
        self.edges.remove(src+trgt)

    def add(self, edge):
        self.edges.add(edge)

G = Graph()
#G.add('ab')
G.add('bc')
G.add('cd')
G.add('bm')
G.add('mc')
G.add('bn')
G.add('nd')
paths=[['a', 'b', 'c', 'd'], ['a', 'b', 'm', 'c', 'd'], ['a', 'b', 'n', 'd']]

paths_to_remove=[]
lenght_paths=len(paths)

for i in range(0,lenght_paths-1,1):
    minCap=sys.maxint
    print 'selected:'
    print paths[i]
    index=0
    lenght_path=len(paths[i])

    for index in range(0,(lenght_path)-1,1):
        id_source=(paths[i])[index]
        id_target=(paths[i])[index+1]

        if G.has_edge(id_source,id_target,key=0):
            cap_edge=G[id_source][id_target][0]['capacity']
            if(cap_edge<minCap):
            minCap=cap_edge

        else: # edge mising
            paths_to_remove.append(paths[i])
            minCap=-1
            break   #i want stop the for index loop

    if(minCap != -1):
        print 'minCap != -1'
        index=0
        for index in range(0,len(paths[i])-1,1):
            id_source=(paths[i])[index]
            id_target=(paths[i])[index+1]
            if G.has_edge(id_source,id_target,key=0):
                old_capacity=G[id_source][id_target][0]['capacity']
                new_capacity=old_capacity-minCap
                G[id_source][id_target][0]['capacity']=new_capacity
                if(new_capacity==0):
                    G.remove_edge(id_source,id_target,key=0)

->你的问题不是休息…

老实说,我也花了一段时间才弄明白!打印以下代码:

path to remove
[]
我所做的唯一修改是将代码缩进函数签名下面的第一行,并在底部调用它! 函数在被调用之前不会运行!LOL:

def feasible_path(G,paths):
    #G is a graph
    #paths is a list of all path in G between two nodes
    paths_to_remove=[]
    lenght_paths=len(paths)

    for i in range(0,lenght_paths-1,1):
        minCap=sys.maxint
        print 'selected:'
        print paths[i]
        index=0
        lenght_path=len(paths[i])

        for index in range(0,(lenght_path)-1,1):
            id_source=(paths[i])[index]
            id_target=(paths[i])[index+1]

            if G.has_edge(id_source,id_target,key=0):
                cap_edge=G[id_source][id_target][0]['capacity']
                if(cap_edge<minCap):
                    minCap=cap_edge

            else: # edge mising
                paths_to_remove.append(paths[i])
                minCap=-1
                break   #i want stop the for index loop

        if(minCap != -1):
            index=0
            for index in range(0,len(paths[i])-1,1):
                id_source=(paths[i])[index]
                id_target=(paths[i])[index+1]
                if G.has_edge(id_source,id_target,key=0):
                    old_capacity=G[id_source][id_target][0]['capacity']
                    new_capacity=old_capacity-minCap
                    G[id_source][id_target][0]['capacity']=new_capacity
                    if(new_capacity==0):
                        G.remove_edge(id_source,id_target,key=0)

    print 'path to remove'
    print paths_to_remove
    return paths
feasible_path("",[])

你能添加一些代码让你的问题更清楚吗?不,这不是break的工作原理,它只是结束了它所处的直接循环。请提供实际重新创建此问题的路径。请确保有足够的路径。在外循环中断后添加打印中断仅中断最里面的循环。我使用完整功能编辑我的答案。我使用完整功能编辑我的问题。为什么它对我不起作用?请尝试路径=['a',b',c',d'],['a',b',m',c',d'],['a',b',n',d']]
path to remove
[]
def feasible_path(G,paths):
    #G is a graph
    #paths is a list of all path in G between two nodes
    paths_to_remove=[]
    lenght_paths=len(paths)

    for i in range(0,lenght_paths-1,1):
        minCap=sys.maxint
        print 'selected:'
        print paths[i]
        index=0
        lenght_path=len(paths[i])

        for index in range(0,(lenght_path)-1,1):
            id_source=(paths[i])[index]
            id_target=(paths[i])[index+1]

            if G.has_edge(id_source,id_target,key=0):
                cap_edge=G[id_source][id_target][0]['capacity']
                if(cap_edge<minCap):
                    minCap=cap_edge

            else: # edge mising
                paths_to_remove.append(paths[i])
                minCap=-1
                break   #i want stop the for index loop

        if(minCap != -1):
            index=0
            for index in range(0,len(paths[i])-1,1):
                id_source=(paths[i])[index]
                id_target=(paths[i])[index+1]
                if G.has_edge(id_source,id_target,key=0):
                    old_capacity=G[id_source][id_target][0]['capacity']
                    new_capacity=old_capacity-minCap
                    G[id_source][id_target][0]['capacity']=new_capacity
                    if(new_capacity==0):
                        G.remove_edge(id_source,id_target,key=0)

    print 'path to remove'
    print paths_to_remove
    return paths
feasible_path("",[])