Skip to content

Instantly share code, notes, and snippets.

@andmax
Last active June 26, 2020 17:08
Show Gist options
  • Save andmax/b83d9c81556b5f87fac11649267e022b to your computer and use it in GitHub Desktop.
Save andmax/b83d9c81556b5f87fac11649267e022b to your computer and use it in GitHub Desktop.
List all neighbor indices at a certain distance from a given pixel position
# The problem is to find all neighbor indices l_neighbors at a distance d from a given pixel position pp in any dimension
# It uses itertools.product to do n-dimensional list cross product
import itertools
pp, d = [2, 2], 2 # Example pp pixel position and d distance
li = [ list(range(ppi-d, ppi+d+1)) for ppi in pp ] # list of each 1-dimensional pp neighbors for each dimension
l_neighbors = [ pli for pli in itertools.product(*li) if any([ p in [l[0], l[-1]] for p, l in zip(pli, li) ]) ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment