question about for cycle

Zentrader zentraders at gmail.com
Sat Sep 29 21:38:09 CEST 2007


On Sep 29, 8:19 am, George Sakkis <george.sak... at gmail.com> wrote:
> 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.
> 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




More information about the Python-list mailing list