question about for cycle
George Sakkis
george.sakkis at gmail.com
Sat Sep 29 11:19:04 EDT 2007
On Sep 29, 10:34 am, "fdu.xia... at gmail.com" <fdu.xia... at gmail.com>
wrote:
> tokl... at gmail.com wrote:
>
> > On 29 sep, 12:04, "fdu.xia... at gmail.com" <fdu.xia... at gmail.com> wrote:
> >
> >> 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
More information about the Python-list
mailing list