473,411 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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 2716
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Matt | last post by:
Hello I have to tables ar and arb, ar holds articles and a swedish description, arb holds descriptions in other languages. I want to retreive all articles that match a criteria from ar and...
3
by: Dennis M. Marks | last post by:
I have a 3 level array. First level is a list of trains. Second level are items about the train. Third level is where there are multiples of the second level item. The search will be of myArray...
6
by: Thomas Beutin | last post by:
Hi, i've a speed problem withe the following statement: SELECT DISTINCT pz.l1_id, pz.l2_id, pz.l3_id, pz.l4_id FROM ot_adresse AS a, ot_produkt AS p LEFT OUTER JOIN ot_kat_prod AS pz ON (...
5
by: Uday Deo | last post by:
Hi everyone, I am looping through 4 nested loops and I would like to break in the inner most loop on certain condition and get the control on the 2 nd loop instead of 3rd loop. Here is briefly...
2
by: dobest03 | last post by:
Hi. Are there any way to access the integer member 'a' of outer structure from inner structure's member function func_inner()? See below the structure... Thanks. struct outer {
9
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
5
by: Kris Kowal | last post by:
I had a thought that might be pepworthy. Might we be able to break outer loops using an iter-instance specific StopIteration type? This is the desired, if not desirable, syntax:: import...
0
by: Luis Zarrabeitia | last post by:
Quoting Kris Kowal <kris.kowal@cixar.com>: I must say, I don't even like the idea of having a 'break', but I kind oflike this proposal. However, it may be ambiguous if the outer and inner...
3
by: ChrisW | last post by:
Hiya, So I have a class that creates threads within it. These threads are a class underneath the parent class. I want to access values in the parent class from the threads while they run. Yet...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.