Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > searching a list of lists as a two-dimensional array?

Reply
Thread Tools

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

 
 
agent-s
Guest
Posts: n/a
 
      02-12-2007
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

 
Reply With Quote
 
 
 
 
James Stroud
Guest
Posts: n/a
 
      02-12-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
 
Reply With Quote
 
 
 
 
Samuel Karl Peterson
Guest
Posts: n/a
 
      02-12-2007
James Stroud <> on Sun, 11 Feb 2007 16:53:16 -0800
didst step forth and proclaim thus:

> 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]])


Wow, maybe it's just me (I'm a pretty bad programmer) but this is
where list comprehensions begin to look unreadable to me. Here's a
C-like way to do it, (warning, untested in python):

for i in range(:
for j in range(:
for offset_i in range(-1,2):
for offset_j in range(-1, 2):
row = i + offset_i
col = j + offset_j
if (row < 0 or row > 7) or (col < 0 or col > \
or ((row,col) == (i,j)):
continue
# else do something with board[row][col]

I realize this is gross and un-Pythonic and does the same thing the
above code does, but it's probably the way I'd choose to do it .
Then again, I've been negatively influenced by doing a game of life in
C a few months back.

--
Sam Peterson
skpeterson At nospam ucdavis.edu
"if programmers were paid to remove code instead of adding it,
software would be much better" -- unknown
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      02-12-2007
En Mon, 12 Feb 2007 02:24:54 -0300, Samuel Karl Peterson
<> escribió:

> James Stroud <> on Sun, 11 Feb 2007 16:53:16 -0800
> didst step forth and proclaim thus:
>
>> 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


> Wow, maybe it's just me (I'm a pretty bad programmer) but this is
> where list comprehensions begin to look unreadable to me. Here's a
> C-like way to do it, (warning, untested in python):


Just for fun, and to add one more way. This is a generator for adjacent
indexes, that can be used to search for occupied cells, for locating a
suitable next move, or whatever:

py> def adj(i, j):
.... for ni in (i-1, i, i+1):
.... if ni not in range(: continue
.... for nj in (j-1, j, j+1):
.... if nj not in range(: continue
.... if ni!=i or nj!=j:
.... yield ni,nj
....
py>
py> print list(adj(4,3))
[(3, 2), (3, 3), (3, 4), (4, 2), (4, 4), (5, 2), (5, 3), (5, 4
py> print list(adj(7,3))
[(6, 2), (6, 3), (6, 4), (7, 2), (7, 4)]
py> print list(adj(4,7))
[(3, 6), (3, 7), (4, 6), (5, 6), (5, 7)]
py> print list(adj(0,7))
[(0, 6), (1, 6), (1, 7)]

--
Gabriel Genellina

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      02-12-2007
En Mon, 12 Feb 2007 02:24:54 -0300, Samuel Karl Peterson
<> escribió:

> James Stroud <> on Sun, 11 Feb 2007 16:53:16 -0800
> didst step forth and proclaim thus:
>
>> 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


> Wow, maybe it's just me (I'm a pretty bad programmer) but this is
> where list comprehensions begin to look unreadable to me. Here's a
> C-like way to do it, (warning, untested in python):


Just for fun, and to add one more way. This is a generator for adjacent
indexes, that can be used to search for occupied cells, for locating a
suitable next move, or whatever:

py> def adj(i, j):
.... for ni in (i-1, i, i+1):
.... if ni not in range(: continue
.... for nj in (j-1, j, j+1):
.... if nj not in range(: continue
.... if ni!=i or nj!=j:
.... yield ni,nj
....
py>
py> print list(adj(4,3))
[(3, 2), (3, 3), (3, 4), (4, 2), (4, 4), (5, 2), (5, 3), (5, 4
py> print list(adj(7,3))
[(6, 2), (6, 3), (6, 4), (7, 2), (7, 4)]
py> print list(adj(4,7))
[(3, 6), (3, 7), (4, 6), (5, 6), (5, 7)]
py> print list(adj(0,7))
[(0, 6), (1, 6), (1, 7)]

--
Gabriel Genellina

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      02-12-2007
"agent-s" <> writes:
> 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


You're getting a bunch of Pythonic suggestions which are easy to
understand though not so efficient. If you're after efficiency you
might look at a book like Welsh and Baczynskyj "Computer Chess II" for
some techniques (warning, this is a rather old book though) and
program in a closer-to-the-metal language. One common approach these
days is "bit boards". Basically you represent the board state as a
bunch of 64-bit words, one bit per square. So for checking occupancy,
you'd have a word having the bits set for occupied squares. If you
only want to check adjacent squares (king moves), you could have a
bunch of bit masks (one for each square) with bits set only for the
adjacent squares (similarly for bishop moves, rook moves, etc.) Then
to check adjacency you'd mask off the appropriate bits for that
square, then AND it against the occupancy word. Normally you'd have
separate occupancy words for your own pieces and your opponent's.
 
Reply With Quote
 
James Stroud
Guest
Posts: n/a
 
      02-12-2007
Paul Rubin wrote:
> "agent-s" <> writes:
>
>>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

>
>
> You're getting a bunch of Pythonic suggestions which are easy to
> understand though not so efficient. If you're after efficiency you
> might look at a book like Welsh and Baczynskyj "Computer Chess II" for
> some techniques (warning, this is a rather old book though) and
> program in a closer-to-the-metal language. One common approach these
> days is "bit boards". Basically you represent the board state as a
> bunch of 64-bit words, one bit per square. So for checking occupancy,
> you'd have a word having the bits set for occupied squares. If you
> only want to check adjacent squares (king moves), you could have a
> bunch of bit masks (one for each square) with bits set only for the
> adjacent squares (similarly for bishop moves, rook moves, etc.) Then
> to check adjacency you'd mask off the appropriate bits for that
> square, then AND it against the occupancy word. Normally you'd have
> separate occupancy words for your own pieces and your opponent's.


In defense of the less efficient suggestions, he did say he had to use a
list of lists. But what you describe is pretty cool.

James
 
Reply With Quote
 
bearophileHUGS@lycos.com
Guest
Posts: n/a
 
      02-12-2007
James Stroud:
> 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]])


Or maybe (untested), Python V.2.5:

srch = [(i,j) for i in [-1,0,1] for j in [-1,0,1] if (i,j) != (0,0)]
is_adj = any(ary[row+i][col+j] for (i,j) in srch)

Or:

is_adj = any(ary[row+i][col+j] for i in [-1,0,1] for j in [-1,0,1] if
(i,j) != (0,0))

Bye,
bearophile

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      02-12-2007
On Feb 12, 4:24 pm, Samuel Karl Peterson
<skpeter...@nospam.please.ucdavis.edu> wrote:
> C-like way to do it, (warning, untested in python):
>
> for i in range(:
> for j in range(:
> for offset_i in range(-1,2):
> for offset_j in range(-1, 2):
> row = i + offset_i
> col = j + offset_j
> if (row < 0 or row > 7) or (col < 0 or col > \
> or ((row,col) == (i,j)):
> continue
> # else do something with board[row][col]
>
> I realize this is gross and un-Pythonic and does the same thing


It's also just a little bit inefficient. Never mind the algorithm,
we'll talk about that later. Let's just improve the code a little:

side_range = range(
delta_range = range(-1, 2)
for i in side_range:
for offset_i in delta_range:
row = i + offset_i
if not (0 <= row <= 7): continue
for j in side_range:
for offset_j in delta_range:
if not offset_i and not offset_j: continue
col = j + offset_j
if not(0 <= col <= 7): continue
# *now* it's safe to do something
# with (row, col)

Such hoisting of code out of inner loops is done for you by a C
compiler -- Python assumes you know what you are doing

Now for the algorithm: all of that testing to see if you are about to
sail off the end of the world is a bit ugly and slow. You can use bit-
bashing, as Paul suggested, even though it's on Steven D'Aprano's list
of 6 deadly sins

Another approach that has been used is to have a 9 x 9 storage area;
the squares off the edge contain a value that is neither "empty" nor
any value that represents a piece. So with careful coding, they
automatically fail tests like "can I capture the piece on this
square" [no, it's not a piece] and "can I move here" [no, it's not
empty]. If the game is chess, you need 10 x 10 to cater for those
pesky knights.

Cheers,
John

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-12-2007
On Sun, 11 Feb 2007 23:20:11 -0800, John Machin wrote:

> Now for the algorithm: all of that testing to see if you are about to
> sail off the end of the world is a bit ugly and slow. You can use bit-
> bashing, as Paul suggested, even though it's on Steven D'Aprano's list
> of 6 deadly sins


Heh. Being a smart-alec is number 7

Seriously, this is Python. Are you *sure* bit-bashing is going to be
faster than the alternative? If this was C, or assembly, you'd almost
certainly be right. But Python is heavily object-oriented, and bit
manipulations are just methods, with all the overhead that entails.

>>> import timeit
>>> timeit.Timer("3 | 2", "").repeat()

[0.33678007125854492, 0.33447504043579102, 0.33331012725830078]
>>> timeit.Timer("3 < 2", "").repeat()

[0.30328893661499023, 0.29070115089416504, 0.28839397430419922]

The problem with bit-bashing, masking etc. is that except for the most
simple cases it is quite obfuscated. If you aren't going to gain a serious
performance boost, why bother?




--
Steven D'Aprano

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
common elements between list of lists and lists antar2 Python 2 07-17-2008 09:19 AM
Google search result to be URL-limited when searching site, but notwhen searching Web stumblng.tumblr Javascript 1 02-04-2008 09:01 AM
How to have a list of lists (or array of lists) bahoo Python 3 04-03-2007 07:37 PM
list of lists of lists .... yomgui Python 6 07-31-2006 07:28 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM



Advertisments