在python中打破一个嵌套循环,打破所有循环

时间:2014-11-12 14:05:43

标签: python loops for-loop 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命令会中断所有循环,但是我想停止内部循环并继续外部循环,在列表路径中继续下一个元素路径。

感谢您的帮助!

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

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

2 个答案:

答案 0 :(得分:1)

paths = [
    [1, 2, 3],
    [3, 4, 5],
]

for path in paths:
    print path
    for edge in path:
        print '\t', edge
        if edge==3:
            print 'break'
            break

输出:

[1, 2, 3]
    1
    2
    3
break
[3, 4, 5]
    3
break

两个中断:因此中断仅停止内循环。

编辑: 使用您的代码和最小的Graph(足以测试您的代码......):

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)

输出:

selected:
['a', 'b', 'c', 'd']
selected:
['a', 'b', 'm', 'c', 'd']

- &GT;你的问题不是休息......

答案 1 :(得分:0)

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

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("",[])