Answers for "get adjacent cells in grid"

1

get adjacent cells in grid

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
Posted by: Guest on March-15-2021

Code answers related to "get adjacent cells in grid"

Python Answers by Framework

Browse Popular Code Answers by Language