1

Is there a method somewhere in a Python package that returns the elements and/ or indexes of an element in a 2d grid. E.g. if we have:

[[1, 2, 3, 4],

 [5, 6, 7, 8],

 [7, 8, 9, 0]]

..and we give the method the index [0,1] it should return [1, 6, 3] (if it could return [[0,0], [1,1], [0,2]] that would be even better) and giving it [1,1] would return [5, 2, 8, 7] (or the corresponding indexes- order isn't important).

There is obviously the simple solution to this, however, it's too slow since I want to do this on a large scale for arrays with several thousand elements. Any suggestions? Thanks in advance.

zarak
  • 2,933
  • 3
  • 23
  • 29
user3396592
  • 363
  • 1
  • 5
  • 15
  • @AnandSKumar: [0,1] points to the element with value 2, who's neighboring values are 1, 6 and 3. – Moberg Sep 03 '15 at 10:45

1 Answers1

0

Why is it too slow?

From the input coordinate [a, b] return the list [[a-1, b], [a+1, b], [a, b-1], [a, b+1]], avoiding coordinates outside your grid.

Moberg
  • 5,253
  • 4
  • 38
  • 54