Conway's game of Life, just because.

Chris Angelico rosuav at gmail.com
Tue May 7 14:35:45 EDT 2019


On Wed, May 8, 2019 at 4:31 AM Eli the Bearded <*@eli.users.panix.com> wrote:
>
> In comp.lang.python, Paul Rubin  <no.email at nospam.invalid> wrote:
>
> Thanks for posting this. I'm learning python and am very familiar with
> this "game".
>
> > #!/usr/bin/python3
> > from itertools import chain
> >
> > def adjacents(cell):            # generate coordinates of cell neighbors
> >     x, y = cell                 # a cell is just an x,y coordinate pair
> >     return ((x+i,y+j) for i in [-1,0,1] for j in [-1,0,1] if i or j)
>
> This line confuses me. How do you expect "if i or j" to work there?

It means that if either i or j has a non-zero value, this will yield
something. With three possible values for i and three for j, it would
normally create nine results; but that condition means it will create
eight, skipping the (0, 0) option. Since the function is meant to give
you all the neighbors of a cell, it makes sense that it shouldn't
return the cell itself.

ChrisA


More information about the Python-list mailing list