searching a list of lists as a two-dimensional array?

James Stroud jstroud at mbi.ucla.edu
Sun Feb 11 19:53:16 EST 2007


agent-s wrote:
> Basically I'm programming a board game and I have to use a list of
> lists to represent the board (a list of 8 lists with 8 elements each).
> I have to search the adjacent cells for existing pieces and I was
> wondering how I would go about doing this efficiently. Thanks
> 

This isn't very clear. What do you mean by "I have to search the 
adjacent cells for existing pieces"?

If piece is 1 and empty is 0 and piece is at ary[row][col]:

import operator
srch = [(i,j) for i in [-1,0,1] for j in [-1,0,1] if (i,j) != (0,0)]
is_adj = reduce(operator.or_, [ary[row+i][col+j] for (i,j) in srch]])


James



More information about the Python-list mailing list