Python,在二维列表中查找邻居

Python,在二维列表中查找邻居,python,list,data-structures,matrix,Python,List,Data Structures,Matrix,问题是,我有一个二维字符列表“T”和“F”,给定坐标,我需要得到它的所有邻居。我有这个: from itertools import product, starmap x, y = (5, 5) cells = starmap(lambda a, b: (x + a, y + b), product((0, -1, +1), (0, -1, +1))) 但是它只会给我一个坐标的列表,所以我仍然会在后面获取值。我希望搜索和检索一步完成,这样findNeighbors(5,5)将返回F,T,F,

问题是,我有一个二维字符列表“T”和“F”,给定坐标,我需要得到它的所有邻居。我有这个:

from itertools import product, starmap
x, y = (5, 5)
cells = starmap(lambda a, b: (x + a, y + b), product((0, -1, +1), (0, -1, +1))) 

但是它只会给我一个坐标的列表,所以我仍然会在后面获取值。我希望搜索和检索一步完成,这样findNeighbors(5,5)将返回F,T,F,F,。。。而不是(5,4)、(5,6)、(4,5)、(4,4)。。。有没有快速的方法?solutin可以包括除列表以外的结构来保存初始信息。以下内容应该可以使用,只需对当前代码进行少量修改:

from itertools import product, starmap, islice

def findNeighbors(grid, x, y):
    xi = (0, -1, 1) if 0 < x < len(grid) - 1 else ((0, -1) if x > 0 else (0, 1))
    yi = (0, -1, 1) if 0 < y < len(grid[0]) - 1 else ((0, -1) if y > 0 else (0, 1))
    return islice(starmap((lambda a, b: grid[x + a][y + b]), product(xi, yi)), 1, None)
为清晰起见,以下是一些没有所有itertools魔力的等效代码:

def findNeighbors(grid, x, y):
    if 0 < x < len(grid) - 1:
        xi = (0, -1, 1)   # this isn't first or last row, so we can look above and below
    elif x > 0:
        xi = (0, -1)      # this is the last row, so we can only look above
    else:
        xi = (0, 1)       # this is the first row, so we can only look below
    # the following line accomplishes the same thing as the above code but for columns
    yi = (0, -1, 1) if 0 < y < len(grid[0]) - 1 else ((0, -1) if y > 0 else (0, 1))
    for a in xi:
        for b in yi:
            if a == b == 0:  # this value is skipped using islice in the original code
                continue
            yield grid[x + a][y + b]
def findNeighbors(网格,x,y):
如果00:
席=(0,1),这是最后一行,所以我们只能看上面。
其他:
席=(0, 1),这是第一行,所以我们只能看下面。
#下一行完成了与上述代码相同的工作,但用于列
yi=(0,-1,1)如果00 else(0,1))
对于xi中的a:
对于易文b:
如果a==b==0:#在原始代码中使用islice跳过此值
持续
收益率网格[x+a][y+b]

您是否能够/愿意使用Numpy?是的,我还不知道,但我可以了解很多问题,如果您想回答,这里还有一个问题,是否有其他数据结构,如Numpy或blist,可以更好地工作。我真的不认识这两个人我只知道他们exist@EasilyBaffled:您可能需要为此创建一个单独的问题。但在任何人回答之前,你必须定义你所说的“更好地工作”是什么意思。这是正确的,非常容易理解,而且考虑到您的数据量很小,几乎可以肯定速度已经足够快了。那么,还有什么比这更好的呢?我不想在标出答案后返回,但是星图((lambda,b:grid[x+a][y+b])),可能会遭受索引超出范围的错误,有人知道如何调整吗?@EasilyBaffled查看我的编辑,它现在也应该可以在网格的边缘上工作。非常感谢,我曾试图编写自己的修复程序,但我仍在努力了解发生了什么
def findNeighbors(grid, x, y):
    if 0 < x < len(grid) - 1:
        xi = (0, -1, 1)   # this isn't first or last row, so we can look above and below
    elif x > 0:
        xi = (0, -1)      # this is the last row, so we can only look above
    else:
        xi = (0, 1)       # this is the first row, so we can only look below
    # the following line accomplishes the same thing as the above code but for columns
    yi = (0, -1, 1) if 0 < y < len(grid[0]) - 1 else ((0, -1) if y > 0 else (0, 1))
    for a in xi:
        for b in yi:
            if a == b == 0:  # this value is skipped using islice in the original code
                continue
            yield grid[x + a][y + b]