breaking out of outer loops

N

noemailplease0001

Any elegant way of breaking out of the outer for loop than below, I
seem to have come across something, but it escapes me

for i in outerLoop:
for j in innerLoop:
if condition:
break
else:
continue
break

Thanks,
K
 
P

Paul Rubin

Any elegant way of breaking out of the outer for loop than below, I
seem to have come across something, but it escapes me

for i in outerLoop:
for j in innerLoop:
if condition:
break
else:
continue
break

You can do it with a try/except/raise statement but I generally prefer
to wrap both loops in a function and use a "return" statement.
 
P

pataphor

On Tue, 29 Jan 2008 11:51:04 -0800 (PST)
Any elegant way of breaking out of the outer for loop than below, I
seem to have come across something, but it escapes me

for i in outerLoop:
for j in innerLoop:
if condition:
break
else:
continue
break

Ha! Think outside the box to begin with ...

P.

def cross(args):
ans = [[]]
for arg in args:
ans = [x+[y] for x in ans for y in arg]
return ans

def test():
L = [[0,1,2]]*3
for a,b,c in cross(L):
print a,b,c

if __name__=='__main__':
test()
 
A

Arnaud Delobelle

Any elegant way of breaking out of the outer for loop than below, I
seem to have come across something, but it escapes me
for i in outerLoop:
   for j in innerLoop:
       if condition:
          break
   else:
       continue
    break

Ha! Think outside the box to begin with ...

P.

def cross(args):
    ans = [[]]
    for arg in args:
        ans = [x+[y] for x in ans for y in arg]
    return ans    

While we're at it, a generator version:

def iproduct(head=None, *tail):
if head is None:
return ((),)
else:
return ((x,)+y for x in head for y in iproduct(*tail))

for a, b, c in iproduct('124', 'ab', 'AB'):
print a, b, c

;-)
 
J

Jeremy Sanders

Any elegant way of breaking out of the outer for loop than below, I
seem to have come across something, but it escapes me

for i in outerLoop:
for j in innerLoop:
if condition:
break
else:
continue
break

Perhaps Python needs a "continue N" or a "break N" statement :)

for i in outerLoop:
for j in innerLoop:
if condition:
break 2

Seeing as we can't have a goto :)

Jeremy
 
D

Diez B. Roggisch

Any elegant way of breaking out of the outer for loop than below, I
seem to have come across something, but it escapes me

for i in outerLoop:
for j in innerLoop:
if condition:
break
else:
continue
break

It's working because for-loops else statements are are executed only if
the loop hasn't been terminated unexpectedly. Which is what happens
here: if the inner loop is breaked, it's else is not executed. So the
outer loop's break is called.

Diez
 
A

Antoon Pardon

Perhaps Python needs a "continue N" or a "break N" statement :)

for i in outerLoop:
for j in innerLoop:
if condition:
break 2

Just fake it with an exception

class LoopBreaker(Exception):
pass

try:
for i in outerLoop:
for j in innerLoop:
if condition:
raise LoopBreaker
except LoopBreaker:
pass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top