[CentralOH] 2016-06-27 ?? Scribbles ??/???: Generatorpalooza I: nested loop word counting nested generators verbose regexes 'C:\new\test' project euler #1 refactoring primes continue statement recursive permutations gcd in musical round 364 twelve days of christmas hypothesis

Neil Ludban nludban at columbus.rr.com
Wed Jul 6 20:58:47 EDT 2016


On Wed, 6 Jul 2016 18:03:04 -0400
jep200404 at columbus.rr.com wrote:
> James Prior presented:
> 
>     A technique for breaking out of nested loops
>     by using a generator to yield tuples of values.
> 
>         http://nbviewer.jupyter.org/github/cohpy/challenge-201605-generators/blob/master/james-prior/b-break-out-of-nested-loops.ipynb
> 
>         Someone from Pillar mentioned another way of breaking out of nested
>         loops by using a return statement.

Yet another way:

Python 2.7.9 (default, Nov 29 2015, 00:47:05) 
[GCC 4.2.1 Compatible FreeBSD Clang 3.4.1 (tags/RELEASE_34/dot1-final 208032)] on freebsd10
Type "help", "copyright", "credits" or "license" for more information.
>>> for x,y,z in ( (x,y,z)
...                for x in range(5)
...                for y in range(x, 5)
...                for z in range(3) ):
...     print x, y, z
...     if x == 2:
...             break
... 
0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 2 2
0 3 0
0 3 1
0 3 2
0 4 0
0 4 1
0 4 2
1 1 0
1 1 1
1 1 2
1 2 0
1 2 1
1 2 2
1 3 0
1 3 1
1 3 2
1 4 0
1 4 1
1 4 2
2 2 0
>>> 


More information about the CentralOH mailing list