Torched90 Torched90 - 1 year ago 187
Python Question

Python 3 ~ Find position of letter in a grid

If I have a grid of strings such as:

grid = ['ABC','DEF','GHI']
I am wondering how to find the coordinates of a letter, for example the letter D would have coordinates

Answer Source
myletter = 'D'
grid = ['ABC','DEF','GHI']
for index, value in enumerate(grid):
    if myletter in value:
        subindex = value.index(myletter)

print [index, subindex]

So what's going on... If you want to loop through a list and access the index while you're at it, the best way to do this is enumerate.

The easiest way to test if something is in a string or a list is just to test if it is in it. That is value in L returns true if the value is in the list L. Finally for lists and strings, L.index(thing) gives you the index of that thing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download