Hi everybody,
Several means to escape a nested loop are given here: http://stackoverflow.com/questions/1...oops-in-python
According to this page, the best way is to modify the loop by affecting the
variables that are tested in the loops. Otherwise, use exception:
"If, for some reason, the terminating conditions can't be worked out,
exceptions are a fall-back plan."
In the following example, is this possible to affect the two iterators to
escape the two loops once one "j" has been printed:
for i in range(5):
for j in range(i):
print j
# I would type "break 2" in shell bash
# In C, I would set j=i-1 and i=4
# In Python, is this possible to affect the two iterators?
Or the only means is to use exception?
Thanks in advance
Julien
--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"
"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke) 5 1688
On Thu, Nov 13, 2008 at 2:07 AM, TP <Tr**********@paralleles.invalidwrote:
Hi everybody,
Several means to escape a nested loop are given here:
http://stackoverflow.com/questions/1...oops-in-python
According to this page, the best way is to modify the loop by affecting the
variables that are tested in the loops. Otherwise, use exception:
"If, for some reason, the terminating conditions can't be worked out,
exceptions are a fall-back plan."
In the following example, is this possible to affect the two iterators to
escape the two loops once one "j" has been printed:
Non-exception alternative:
done = False
for i in range(5):
for j in range(i):
print j
done = True
break
# I would type "break 2" in shell bash
# In C, I would set j=i-1 and i=4
# In Python, is this possible to affect the two iterators?
if done:
break
>
Or the only means is to use exception?
No, you could add a boolean variable and a break condition like above.
Cheers,
Chris
--
Follow the path of the Iguana... http://rebertia.com
>
Thanks in advance
Julien
--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"
"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
-- http://mail.python.org/mailman/listinfo/python-list
On Thu, 13 Nov 2008 11:07:25 +0100, TP wrote:
According to this page, the best way is to modify the loop by affecting
the variables that are tested in the loops. Otherwise, use exception:
"If, for some reason, the terminating conditions can't be worked out,
exceptions are a fall-back plan."
In the following example, is this possible to affect the two iterators
to escape the two loops once one "j" has been printed:
for i in range(5):
for j in range(i):
print j
# I would type "break 2" in shell bash # In C, I would set j=i-1
and i=4
# In Python, is this possible to affect the two iterators?
Or the only means is to use exception?
You could put the code into its own, maybe local, function and use
``return``.
Ciao,
Marc 'BlackJack' Rintsch
TP wrote:
Hi everybody,
Several means to escape a nested loop are given here:
http://stackoverflow.com/questions/1...oops-in-python
>
According to this page, the best way is to modify the loop by affecting
the variables that are tested in the loops. Otherwise, use exception:
"If, for some reason, the terminating conditions can't be worked out,
exceptions are a fall-back plan."
In the following example, is this possible to affect the two iterators to
escape the two loops once one "j" has been printed:
for i in range(5):
for j in range(i):
print j
# I would type "break 2" in shell bash
# In C, I would set j=i-1 and i=4
# In Python, is this possible to affect the two iterators?
Or the only means is to use exception?
Here's one way to turn multiple iterators into a single one:
def pairs(n):
for i in range(n):
for k in range(i):
yield i, k
for i, k in pairs(5):
print i, k
I've yet to see an example where a multi-level break would improve the
code's readability. It is typically a speed hack.
Peter
PS: Have a look into the itertools if you consider that approach.
Chris Rebert wrote:
On Thu, Nov 13, 2008 at 2:07 AM, TP <Tr**********@paralleles.invalidwrote:
>Hi everybody,
Several means to escape a nested loop are given here:
http://stackoverflow.com/questions/1...oops-in-python
According to this page, the best way is to modify the loop by affecting the variables that are tested in the loops. Otherwise, use exception:
"If, for some reason, the terminating conditions can't be worked out, exceptions are a fall-back plan."
In the following example, is this possible to affect the two iterators to escape the two loops once one "j" has been printed:
Non-exception alternative:
done = False
>for i in range(5): for j in range(i): print j
done = True
break
> # I would type "break 2" in shell bash # In C, I would set j=i-1 and i=4 # In Python, is this possible to affect the two iterators?
if done:
break
>Or the only means is to use exception?
No, you could add a boolean variable and a break condition like above.
Though I would have to ask why you would want to. An exception seems
rather cleaner, though of course tastes vary.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
On Thu, 13 Nov 2008 02:16:32 -0800, Chris Rebert wrote:
On Thu, Nov 13, 2008 at 2:07 AM, TP <Tr**********@paralleles.invalid>
wrote:
>> In the following example, is this possible to affect the two iterators to escape the two loops once one "j" has been printed:
Non-exception alternative:
done = False
for i in range(5):
for j in range(i):
print j
done = True
break
if done:
break
Another alternative is explicitly jumping in the outer loop::
for i in range(5): # use xrange for larger ranges
for j in range(i):
print j
break
else:
continue # applies already to the outer loop
break
HTH,
--
Robert "Stargaming" Lehmann This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jon LaRosa |
last post by:
I'm am trying to remove multiple line breaks ("\n"). Here's my code:
$imageStr = trim($_POST);
$imageStr = str_replace("\n\n", "\n", $imageStr);
----------
$_POST is a textarea field, and...
|
by: Ralph Freshour |
last post by:
I'm not sure the follow multiple table query is the right way to do
what I need to do although it seems to be working:
$php_SQL = "SELECT * ".
"FROM basics, personal, photos ".
"WHERE...
|
by: Rob |
last post by:
I have an ASP.NET application that uses forms-based
authentication. A user wishes to be able to run multiple
sessions of this application simultaneously from the
user's client machine.
The...
|
by: Rich Kucera |
last post by:
Holding all versions at 5.0.4, Multiple stacks with multiple-version
configurations inevitable
Will have to wait to see what the impact of problems such as
http://bugs.php.net/bug.php?id=33643 ...
|
by: cvisal |
last post by:
Hi all
Im working on productivity calculations (Time calculations) and need
some help in coding.
Database Tool:MS-Access 2003.
The general operator punch-in time is 5:30 AM and the punch-out...
|
by: cimple |
last post by:
I've always made sure never to have multiple return paths in a function
because i thought it produced bigger code (and for readability). I
remember Lippman from Inside the Object Model saying...
|
by: m.shidoshi |
last post by:
I was recently put in charge of heading up my company's website, and
while I have a lot of experience on the design side of things, I'm
still very new to the programming side. When I started, the...
|
by: Geometer |
last post by:
Hello, and good whatever daytime is at your place..
please can somebody tell me, what the standard behavior of strtok shall be,
if it encounters two or more consecutive delimiters like in...
|
by: Bart Van der Donck |
last post by:
Hello,
I'm having a hard time trying to configure printed output that consists
of multiple pages. The idea is the following:
<div style="
border: 1px solid blue;
position: absolute;
top:...
|
by: jbedwe02 |
last post by:
Hi all,
I have no clue how to slove this problem & am desperate for an answer:
I'm creating a spreadsheet and need to now how to seperate data within a single cell into multiple cells.
The...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
| |