Hi all,
I have the following code:
for i in generator_a: # the first "for" cycle
for j in generator_b:
if something_happen:
# do something here ..., I want the outer cycle to break
break
What should I do if I want the outer "for" cycle to continue or break ? If I
put a "continue" or "break" in the inner cycle, it has no effect on the outer
cycle.
And I have another question. Which is the most efficient way to check if there
are duplicate items in a list ? The items in the list may cannot be hashed, so
set() may not work on the list.
Regards, 10 2062
On Sep 29, 11:04 am, "fdu.xia...@gmail.com" <fdu.xia...@gmail.com>
wrote:
....
What should I do if I want the outer "for" cycle to continue or break ? If I
put a "continue" or "break" in the inner cycle, it has no effect on the outer
cycle.
I'd also be interested in the idiomatic solution to this one. I can
see a number of solutions, from the ugly:
for i in range(10):
do_break = True
for j in range(10):
if j == 6:
break
else:
do_break = False
if do_break:
break
This will break the outer loop if the inner loop exited with a break.
Using exceptions:
for i in range(10):
try:
for j in range(10):
print i, j
if j == 6:
raise MyException
except MyException, e:
break # or continue or whatever.
Encapsulating in a function and using return:
def get_value():
for i in range(10):
for j in range(10):
print i, j
if j == 6:
return fn(i, j)
I guess to an extent it would depend on the exact situation as to
which of these is more suitable. Are there any other recommended
solutions to this?
--
Ant...
Ant wrote:
On Sep 29, 11:04 am, "fdu.xia...@gmail.com" <fdu.xia...@gmail.com>
wrote:
...
>What should I do if I want the outer "for" cycle to continue or break ? If I put a "continue" or "break" in the inner cycle, it has no effect on the outer cycle.
I'd also be interested in the idiomatic solution to this one. I can
see a number of solutions, from the ugly:
for i in range(10):
do_break = True
for j in range(10):
if j == 6:
break
else:
do_break = False
if do_break:
break
Here's a variant that doesn't need the flag
>>inner = "abc" outer = "xbz" for i in outer:
.... for k in inner:
.... if i == k:
.... print "found", i
.... break
.... else:
.... print i, "not found"
.... continue
.... break
....
x not found
found b
but I usually prefer a helper function like this
def get_value():
for i in range(10):
for j in range(10):
print i, j
if j == 6:
return fn(i, j)
or this:
>>def f(i, inner):
.... for k in inner:
.... if i == k:
.... print "found", i
.... return True
....
>>for i in outer:
.... if f(i, inner):
.... break
.... print i, "not found"
....
x not found
found b
Peter
Ant <an****@gmail.comwrote:
On Sep 29, 11:04 am, "fdu.xia...@gmail.com" <fdu.xia...@gmail.com>
wrote:
...
>What should I do if I want the outer "for" cycle to continue or break ? If I put a "continue" or "break" in the inner cycle, it has no effect on the outer cycle.
....
I guess to an extent it would depend on the exact situation as to
which of these is more suitable. Are there any other recommended
solutions to this?
I think the other Pythonic option you have missed is to convert the nested
for loops into a single loop by writing a generator.
def ranges(limit1, limit2):
range1, range2 = range(limit1), range(limit2)
for i in range1:
for j in range2:
yield i,j
....
for i, j in ranges(10, 10):
... whatever ...
if j==6:
break
On Sep 29, 8:04 pm, "fdu.xia...@gmail.com" <fdu.xia...@gmail.com>
wrote:
[snip]
And I have another question. Which is the most efficient way to check if there
are duplicate items in a list ? The items in the list may cannot be hashed, so
set() may not work on the list.
The following classic by Tim Peters answers the question "How do I
remove duplicate items from a list". http://aspn.activestate.com/ASPN/Coo...n/Recipe/52560
It should help -- provided of course that you mean "duplicates" in the
sense that two Python objects a and b are duplicates iff a == b.
If you mean duplicates in a fuzzier sense e.g. "Mao Zedong" and "Mao
Tse-Tung", the problem gets harder ...
HTH,
John
On 29 sep, 12:04, "fdu.xia...@gmail.com" <fdu.xia...@gmail.comwrote:
for i in generator_a: # the first "for" cycle
for j in generator_b:
if something_happen:
# do something here ..., I want the outer cycle to break
break
Do you like this?
generator_ab = ((x, y) for x in generator_a for y in generator_b)
for i, j in generator_ab:
if condition:
# do something
break to*****@gmail.com wrote:
On 29 sep, 12:04, "fdu.xia...@gmail.com" <fdu.xia...@gmail.comwrote:
>for i in generator_a: # the first "for" cycle for j in generator_b: if something_happen: # do something here ..., I want the outer cycle to break break
Do you like this?
generator_ab = ((x, y) for x in generator_a for y in generator_b)
for i, j in generator_ab:
if condition:
# do something
break
In this case, the tuple generator_ab must be generated first. Sometime
it maybe a waste to generate all possible combinations of i,j first.
I have googled and found the PEP-3136(Labeled break and continue),
which has been rejected. I have also read why Guido rejected this PEP( http://mail.python.org/pipermail/pyt...y/008663.html),
but I still think labeled break and continue is a good feature in my
case.
Regards,
On Sep 29, 10:34 am, "fdu.xia...@gmail.com" <fdu.xia...@gmail.com>
wrote:
tokl...@gmail.com wrote:
On 29 sep, 12:04, "fdu.xia...@gmail.com" <fdu.xia...@gmail.comwrote:
>
>for i in generator_a: # the first "for" cycle
> for j in generator_b:
> if something_happen:
> # do something here ..., I want the outer cycle to break
> break
>
Do you like this?
>
generator_ab = ((x, y) for x in generator_a for y in generator_b)
for i, j in generator_ab:
if condition:
# do something
break
>
In this case, the tuple generator_ab must be generated first.
It's not a tuple, it is a generator expression that can generate
tuples *lazily*.
Sometime it maybe a waste to generate all possible combinations of i,j first.
It doesn't; read about generator expressions at http://www.python.org/dev/peps/pep-0289/
George
On Sep 29, 8:19 am, George Sakkis <george.sak...@gmail.comwrote:
On Sep 29, 10:34 am, "fdu.xia...@gmail.com" <fdu.xia...@gmail.com>
wrote:
tokl...@gmail.com wrote:
On 29 sep, 12:04, "fdu.xia...@gmail.com" <fdu.xia...@gmail.comwrote:
>for i in generator_a: # the first "for" cycle
> for j in generator_b:
> if something_happen:
> # do something here ..., I want the outer cycle to break
> break
Do you like this?
generator_ab = ((x, y) for x in generator_a for y in generator_b)
for i, j in generator_ab:
if condition:
# do something
break
In this case, the tuple generator_ab must be generated first.
George
You can get specific break points by expanding the for loop into a
while loop, and this is perhaps why it has never been implemented with
for loops.
ctr_a=0
ctr_b=0
while ctr_a < len(generator_a):
this_el_a = generator_a[ctr_a]
while ctr_b < len(generator_b):
this_el_b = generator_b[ctr_ b]
if something_happen:
ctr_b = len(generator_b) ## break this loop
if something_else:
ctr_a = len(generator_a) ## break outer while loop
ctr_b += 1
ctr_a += 1
George Sakkis wrote:
On Sep 29, 10:34 am, "fdu.xia...@gmail.com" <fdu.xia...@gmail.com>
wrote:
>tokl...@gmail.com wrote:
> On 29 sep, 12:04, "fdu.xia...@gmail.com" <fdu.xia...@gmail.comwrote:
for i in generator_a: # the first "for" cycle for j in generator_b: if something_happen: # do something here ..., I want the outer cycle to break break
Do you like this?
generator_ab = ((x, y) for x in generator_a for y in generator_b) for i, j in generator_ab: if condition: # do something break
In this case, the tuple generator_ab must be generated first.
It's not a tuple, it is a generator expression that can generate
tuples *lazily*.
>Sometime it maybe a waste to generate all possible combinations of i,j first.
It doesn't; read about generator expressions at http://www.python.org/dev/peps/pep-0289/
George
Thanks, I didn't realize that.
However, I still think labeled break and continue is a valuable feature, which
is easier to understand and to use.
"fd********@gmail.com" <fd********@gmail.comwrote:
>>Sometime it maybe a waste to generate all possible combinations of i,j first.
It doesn't; read about generator expressions at http://www.python.org/dev/peps/pep-0289/
George
Thanks, I didn't realize that.
However, I still think labeled break and continue is a valuable
feature, which is easier to understand and to use.
Can you come up with a realistic example where you think a labelled break
would be easier to understand and use? A concrete example would really help
if you hope to persuade anyone that your way is better.
I find that Python's ability to extract the control part of a loop into a
generator is an extremely powerful way to make loops much clearer for
several reasons: you separate completely the 'what we are looping over',
and 'when do we stop' from the 'what do we then do with each item'; also
you have an opportunity to give the code controlling the loop a separate
(and meaningful) name. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jessica |
last post by:
Hi,
I do not have a lot of experience with STL and I hope some of you
might be able to help me on this seemingly elementary question.
I have a vector of doubles (v1). I am trying to copy the...
|
by: Tim Rentsch |
last post by:
I have a question about what ANSI C allows/requires in a particular
context related to 'volatile'. Consider the following:
volatile int x;
int
x_remainder_arg( int y ){
return x % y;
}
|
by: me |
last post by:
Hi All,
I am new to Classes and learniing the ropes with VB.NET express
Here's my question - say I have a want to manage a list of books.
Each book has an Author, Title and ISBN
Now, I am...
|
by: junky_fellow |
last post by:
Hi,
Is there any efficient way of finding the intesection point of two
singly linked lists ?
I mean to say that, there are two singly linked lists and they meet at
some point. I want to find...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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...
| |