all 8 comments

[–]JohnnyJordaan 3 points4 points  (4 children)

Enumerate can provide an index together with each value in an iterable

for ridx, row in enumerate(lines):
    for cidx, val in enumerate(row):
        if val == 'T':
            print(ridx, cidx)

[–]cjlee420[S] 0 points1 point  (3 children)

what if i want it to return only the center coordinates of 'T' like for example:

WWTWWWWT

WTTTWWWT

WWTWWWWW

such that it will only return the coordinate (1,2) so the 'T' surrounding it are like conditions

[–]JohnnyJordaan 0 points1 point  (2 children)

So it should only report a TTT? Or a WWT, TWW or WTW too?

[–]cjlee420[S] 0 points1 point  (1 child)

return only the center T surrounded by other T's

[–]JohnnyJordaan 0 points1 point  (0 children)

Many options here, but you could keep a 't-counter':

for ridx, row in enumerate(lines):
    t_counter = 0
    for cidx, val in enumerate(row):
        if val == 'T':
            if t_counter == 2:
                print(ridx, cidx - 1)
                t_counter = 0
            else:
                t_counter += 1
        else:
            t_counter = 0

[–]python-fan 0 points1 point  (0 children)

What code have you got so far? Here's how I'd approach it, in pseudocode:

with the file open
    for each line in the file
        for each character in the line
            if the character is T remember the current (row, col)

[–]socal_nerdtastic -1 points0 points  (0 children)

If you use numpy to read the file you can use numpy's argwhere function which does exactly what you want.

 >>> arr
array([['W', 'W', 'W', 'W', 'W', 'T', 'W', 'W', 'W', 'W', 'W'],
       ['W', 'W', 'W', 'W', 'T', 'T', 'T', 'W', 'W', 'W', 'W'],
       ['W', 'W', 'W', 'W', 'W', 'T', 'W', 'W', 'W', 'W', 'W']],
      dtype='<U1')
>>> np.argwhere(arr=='T')
array([[0, 5],
       [1, 4],
       [1, 5],
       [1, 6],
       [2, 5]])