468,537 Members | 2,404 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,537 developers. It's quick & easy.

breaking out of outer loops

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
Jan 29 '08 #1
6 2589
no***************@gmail.com writes:
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.
Jan 29 '08 #2
On Tue, 29 Jan 2008 11:51:04 -0800 (PST)
no***************@gmail.com wrote:
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()
Jan 29 '08 #3
On Jan 29, 8:55*pm, pataphor <patap...@gmail.comwrote:
On Tue, 29 Jan 2008 11:51:04 -0800 (PST)

noemailplease0...@gmail.com wrote:
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

;-)

--
Arnaud

Jan 29 '08 #4
no***************@gmail.com wrote:
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

--
Jeremy Sanders
http://www.jeremysanders.net/
Jan 29 '08 #5
no***************@gmail.com schrieb:
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
Jan 29 '08 #6
Jeremy Sanders wrote:
no***************@gmail.com wrote:
>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 :-)
Not true! You just have to import it:

http://entrian.com/goto/

STeVe
Jan 30 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Matt | last post: by
3 posts views Thread by Dennis M. Marks | last post: by
9 posts views Thread by Matthias Buelow | last post: by
5 posts views Thread by Kris Kowal | last post: by
reply views Thread by Luis Zarrabeitia | last post: by
3 posts views Thread by ChrisW | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.