将列表列表作为二维数组进行搜索? [英] searching a list of lists as a two-dimensional array?
问题描述
基本上我正在编写棋盘游戏,我必须使用
列表列表来代表棋盘(8个列表的列表,每个8个元素)。
我必须在相邻的单元格中查找现有的碎片,我想知道如何有效地进行此操作。谢谢
agent-s写道:
基本上我是'我正在编写一个棋盘游戏,我必须使用一个
列表来代表棋盘(8个列表,每个列表包含8个元素)。
我必须在相邻的单元格中搜索现有的碎片,我想知道如何有效地进行此操作。谢谢
这不是很清楚。你是什么意思我必须搜索
相邻单元格中的现有作品?
如果棋子是1而且空是0且棋子是at ary [row] [col]:
导入运算符
srch = [(i,j)for i in [-1,0,1]对于[-1,0,1]中的j如果(i,j)!=(0,0)]
is_adj = reduce(operator.or_,[ary [row + i] [col] + j] for(i,j)in srch]])
James
James Stroud< js ***** @ mbi .ucla.eduon Sun,2007年2月11日16:53:16 -0800
向前迈出了一步并宣布:
agent-s写道:
基本上我正在编写一个棋盘游戏,我必须使用
列表来代表棋盘(a 8个列表的列表,每个列表包含8个元素。
我必须在相邻的单元格中搜索现有的部分,而且我想知道如何有效地执行此操作。谢谢
这不是很清楚。你是什么意思我必须搜索
相邻单元格中的现有作品?
如果棋子是1而且空是0且棋子是at ary [row] [col]:
导入运算符
srch = [(i,j)for i in [-1,0,1]对于[-1,0,1]中的j如果(i,j)!=(0,0)]
is_adj = reduce(operator.or_,[ary [row + i] [col] + j] for(i,j)in srch]])
哇,也许这只是我(我是一个非常糟糕的程序员)但是这个
其中列表推导对我来说开始变得难以理解。这是一个
C-like方式,(警告,在python中未经测试):
for i in range(8):
表示范围内的j(8):
表示offset_i的范围(-1,2):
表示offset_j的范围(-1,2) ):
row = i + offset_i
col = j + offset_j
if(row< 0或row 7)or(col< ; 0或col 8)\
或((row,col)==(i,j)):
继续
#或者做一些事情[row] [col]
我意识到这是粗略的和非Pythonic并且做同样的事情,上面代码的
,但这可能就是我选择这样做的方式:)。
然后再次,我已经被生活中的一场游戏所带来负面影响。
$ b几个月前$ B C.
-
Sam Peterson
skpeterson在nospam ucdavis.edu
如果程序员付费删除代码而不是添加代码,
软件会更好 - 未知
En Mon,2007年2月12日02:24:54 -0300,Samuel Karl Peterson
< sk **** ****@nospam.please.ucdavis.eduescribió:
James Stroud< js ***** @ mbi.ucla.eduon Sun,2007年2月11日16:53:16 -0800
向前迈出了一步并宣布:
> agent-s写道:
< blockquote class =post_quotes>
基本上我正在编写一个棋盘游戏,我必须使用
列表来表示棋盘(8个列表包含8个元素每个)。
我必须在相邻的单元格中查找现有的碎片,而且我想知道如何有效地进行此操作。谢谢
哇,也许这只是我(我是一个非常糟糕的程序员)但这是
其中列表推导对我来说开始变得难以理解。这是一个
类似C的方式,(警告,在python中未经测试):
只是为了好玩,并且为了再添一个方法。这是一个用于相邻
索引的生成器,可用于搜索占用的单元格,用于定位适合下一步移动的
等等:
pydef adj(i,j):
....对于ni in(i-1,i,i + 1):
。 ...如果ni不在范围内(8):继续
....对于(j-1,j,j + 1)中的nj:
.. ..如果nj不在范围内(8):继续
....如果ni!= i或nj!= j:
....产生ni, nj
....
py>
pyprint list(adj(4,3))
[ (3,2),(3,3),(3,4),(4,2),(4,4),(5,2),(5,3),(5,4 /
pyprint list(adj(7,3))
[(6,2),(6,3),(6,4),(7,2),(7, 4)]
pyprint list(adj(4,7))
[(3,6),(3,7),(4,6),(5 ,6),(5,7)]
pyprint list(adj(0,7))
[(0,6),(1,6),( 1,7)]
-
Gabriel Genellina
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
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
James Stroud <js*****@mbi.ucla.eduon 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(8):
for j in range(8):
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 8) \
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
En Mon, 12 Feb 2007 02:24:54 -0300, Samuel Karl Peterson
<sk********@nospam.please.ucdavis.eduescribió:
James Stroud <js*****@mbi.ucla.eduon 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:
pydef adj(i, j):
.... for ni in (i-1, i, i+1):
.... if ni not in range(8): continue
.... for nj in (j-1, j, j+1):
.... if nj not in range(8): continue
.... if ni!=i or nj!=j:
.... yield ni,nj
....
py>
pyprint list(adj(4,3))
[(3, 2), (3, 3), (3, 4), (4, 2), (4, 4), (5, 2), (5, 3), (5, 4
pyprint list(adj(7,3))
[(6, 2), (6, 3), (6, 4), (7, 2), (7, 4)]
pyprint list(adj(4,7))
[(3, 6), (3, 7), (4, 6), (5, 6), (5, 7)]
pyprint list(adj(0,7))
[(0, 6), (1, 6), (1, 7)]
--
Gabriel Genellina
这篇关于将列表列表作为二维数组进行搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!