确定单元二维列表的邻居

Dom*_*mra 10 python matrix

我有一个列表列表,类似于

[[1, 2, 3,],[4, 5, 6,],[7, 8, 9]].

以图形方式表示为:

1 2 3
4 5 6
7 8 9
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种优雅的方法来检查单元格的邻居值,水平,垂直和对角线.例如,[0] [2]的邻居是[0] [1],[1] [1]和[1] [2]或者数字2,5,6.

现在我意识到我可以做一个暴力攻击检查每个值a la:

[i-1][j]
[i][j-1]
[i-1][j-1]
[i+1][j]
[i][j+1]
[i+1][j+1]
[i+1][j-1]
[i-1][j+1]
Run Code Online (Sandbox Code Playgroud)

但这很简单,我想通过看一些更优雅的方法我可以学到更多东西.

tru*_*ppo 18

# Size of "board"
X = 10
Y = 10

neighbors = lambda x, y : [(x2, y2) for x2 in range(x-1, x+2)
                               for y2 in range(y-1, y+2)
                               if (-1 < x <= X and
                                   -1 < y <= Y and
                                   (x != x2 or y != y2) and
                                   (0 <= x2 <= X) and
                                   (0 <= y2 <= Y))]

>>> print(neighbors(5, 5))
[(4, 4), (4, 5), (4, 6), (5, 4), (5, 6), (6, 4), (6, 5), (6, 6)]
Run Code Online (Sandbox Code Playgroud)

我不知道这是否被认为是干净的,但是这个单行通过迭代它们并丢弃任何边缘情况给你所有的邻居.


joh*_*omp 9

mb个...

from itertools import product, starmap

x, y = (8, 13)
cells = starmap(lambda a,b: (x+a, y+b), product((0,-1,+1), (0,-1,+1)))

// [(8, 12), (8, 14), (7, 13), (7, 12), (7, 14), (9, 13), (9, 12), (9, 14)]
print(list(cells)[1:])
Run Code Online (Sandbox Code Playgroud)


etu*_*rdu 8

假设你有一个方阵:

from itertools import product

size = 3

def neighbours(cell):
    for c in product(*(range(n-1, n+2) for n in cell)):
        if c != cell and all(0 <= n < size for n in c):
            yield c
Run Code Online (Sandbox Code Playgroud)

使用itertools.product并感谢Python的yield表达式星形运算符,该函数非常干燥但仍然可读.

给定矩阵大小为3,然后您可以(如果需要)收集邻居list:

>>> list(neighbours((2,2)))
[(1, 1), (1, 2), (2, 1)]
Run Code Online (Sandbox Code Playgroud)

该功能的作用可视化如下:

功能可视化


Joh*_*ooy 5

for x_ in range(max(0,x-1),min(height,x+2)):
  for y_ in range(max(0,y-1),min(width,y+2)):
    if (x,y)==(x_,y_): continue
    # do stuff with the neighbours

>>> a=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> width=height=3
>>> x,y=0,2
>>> for x_ in range(max(0,x-1),min(height,x+2)):
...   for y_ in range(max(0,y-1),min(width,y+2)):
...     if (x,y)==(x_,y_): continue
...     print a[x_][y_]
... 
2
5
6
Run Code Online (Sandbox Code Playgroud)


Cor*_*vin 5

如果有人对选择直接(非对角线)邻居的替代方法感到好奇,那么你去吧:

neighbors = [(x+a[0], y+a[1]) for a in 
                    [(-1,0), (1,0), (0,-1), (0,1)] 
                    if ( (0 <= x+a[0] < w) and (0 <= y+a[1] < h))]
Run Code Online (Sandbox Code Playgroud)