📜  获取网格中的相邻单元格 - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:20.440000             🧑  作者: Mango

代码示例1
x = 5
y = 5
#just add the target x and y to row and col to get adjacent positions
adjacent_positions = [[row + x ,col + y] for row in (-1,0,1) for col in (-1,0,1) if [row + x ,col + y] != [x,y]]
#output
#[[4, 4], [4, 5], [4, 6], [5, 4], [5, 6], [6, 4], [6, 5], [6, 6]]

#NOTE: without the if statement would produce a list including the target x and y