2D Python列表中每个项的迭代器及其直接m×n邻域

2D Python列表中每个项的迭代器及其直接m×n邻域,python,list,iterator,Python,List,Iterator,我需要生成一个迭代器,该迭代器将在Python2D数组上进行迭代,并在MxN邻域中生成每个项及其周围的所有项 例如,给定棋盘模式中0和1的列表,我需要一个迭代器对象,该对象将生成3x3邻域,例如: [0,1,0], [1,0,1], [0,1,0] 注意:收益率不需要是另一个数组,但最好能够引用位置/索引相对于中心项的邻居,或者至少是相对于彼此的邻居 提前谢谢 编辑:到目前为止,我一直试图做它完全由索引,即 for x in range(len(S)): for y in range(

我需要生成一个迭代器,该迭代器将在Python2D数组上进行迭代,并在MxN邻域中生成每个项及其周围的所有项

例如,给定棋盘模式中0和1的列表,我需要一个迭代器对象,该对象将生成3x3邻域,例如:

[0,1,0],
[1,0,1],
[0,1,0]
注意:收益率不需要是另一个数组,但最好能够引用位置/索引相对于中心项的邻居,或者至少是相对于彼此的邻居

提前谢谢

编辑:到目前为止,我一直试图做它完全由索引,即

for x in range(len(S)):
    for y in range(len(S[0])):
        for i in range(-1,2):
            for j in range(-1,2):
                #access neighbour with S[x+i][y+j]
通过以下列表解决性能方面的问题:

board = [[1,0]*2000]*1000

运行时间基本上与电路板为10x10时相同。通过将元素存储在一维列表中,并根据阵列的逻辑宽度和高度计算偏移基数,通常可以快速访问二维阵列。它通常可以简化计算和边界检查,这只需要在内部处理一维

class Board(object):
    def __init__(self, width, height):
        self.height = height
        self.width = width
        self.size = width*height
        self.board = [i%2 for i in xrange(self.size)] # checkerboard init

    def __getitem__(coords):
        """ get board[x, y] """
        offset = coords[1]*self.width + coords[0]
        return self.board[offset]

    def __setitem__(coords, value):
        """ set board[x, y] = value """
        offset = coords[1]*self.width + coords[0]
        self.board[offset] = value

    def __str__(self):
        lines = []
        for y in xrange(self.height):
            offset = y*self.width
            row = self.board[offset:offset+self.width]
            lines.append(','.join(str(v) for v in row))
        return ',\n'.join(lines)

    def neighbourhood(self, x, y):
        position = y*self.width + x
        for offset in [
            position-self.width-1, position-self.width, position-self.width+1,
            position-1,                                 position+1,
            position+self.width-1, position+self.width, position+self.width+1]:
            if -1 < offset < self.size:
                yield self.board[offset]

board = Board(5, 5)
print board
print
print [value for value in board.neighbourhood(0, 0)]
print [value for value in board.neighbourhood(2, 2)]

给我们看看你试过的代码。谢谢。只是一个简单的问题。如果我需要为4000x2000“电路板”上的每个项目找到邻居,这是一种有效的方法吗?我想说是的,它根本不检查整个电路板,只检查其周围的单元。@user2515310如果通过在每个方向上移出半径量来工作,它不会在电路板上迭代。如果这回答了你的问题,你应该考虑接受它。不管董事会规模如何,运行时间基本上是相同的。换言之,它无论如何都是缓慢的。另外,GetNeighbor不是迭代器,尽管它返回的是可写的东西。是的,但至少在大多数域中,上面的过程并不慢
board = [[1,0]*2000]*1000
class Board(object):
    def __init__(self, width, height):
        self.height = height
        self.width = width
        self.size = width*height
        self.board = [i%2 for i in xrange(self.size)] # checkerboard init

    def __getitem__(coords):
        """ get board[x, y] """
        offset = coords[1]*self.width + coords[0]
        return self.board[offset]

    def __setitem__(coords, value):
        """ set board[x, y] = value """
        offset = coords[1]*self.width + coords[0]
        self.board[offset] = value

    def __str__(self):
        lines = []
        for y in xrange(self.height):
            offset = y*self.width
            row = self.board[offset:offset+self.width]
            lines.append(','.join(str(v) for v in row))
        return ',\n'.join(lines)

    def neighbourhood(self, x, y):
        position = y*self.width + x
        for offset in [
            position-self.width-1, position-self.width, position-self.width+1,
            position-1,                                 position+1,
            position+self.width-1, position+self.width, position+self.width+1]:
            if -1 < offset < self.size:
                yield self.board[offset]

board = Board(5, 5)
print board
print
print [value for value in board.neighbourhood(0, 0)]
print [value for value in board.neighbourhood(2, 2)]