Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

Comment

PREVIOUS NEXT
Code Example
Python :: b1-motion tkinter 
Python :: get gpu name tensorflow and pytorch 
Python :: python random choice in list 
Python :: how to get rid of all null values in array python 
Python :: how to increase size of graph in jupyter 
Python :: build url python 
Python :: python keyboard press 
Python :: change column value based on another column pandas 
Python :: nested dict to df 
Python :: how to run a function in interval in python 
Python :: Python find max in list of dict by value 
Python :: django wait for database 
Python :: tqdm parallel 
Python :: tkinter gui grid and frame 
Python :: tkinter change button text 
Python :: string to float python pandas 
Python :: colab read xlsx 
Python :: distribution plot with curve python 
Python :: how to check if everything inside a list is unique 
Python :: how to convert png to pdf with python 
Python :: stringbuilder python 
Python :: password combination python 
Python :: huggingface default cache dir 
Python :: list of strings to numbers python 
Python :: say command python 
Python :: python time in nanoseconds 
Python :: how to increment date by one in python 
Python :: how to count in a loop python 
Python :: get env variable linux python 
Python :: get last element of array python 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =