Using list comprehension to search a 2d array (python)
Question: I am trying to search a 2D array of characters and return the array indices,
(x_T,y_T), of all of the letter T's in the array. I figure this could easily
done with two stacked for loops but I was curious to know if it could be done
my efficiently using list comprehension. I have tried things like:

    
    
    TPos = [[x, y] for x in range(0, len(array[x])) for y in range(0, len(array[x][y])) if array[x][y] == 'T']
    

But I am getting errors to do with array bounds. Could someone point me in the
right direction. Cheers, Jack

# Edit

Now trying to use 'ndenumerate' like so:

    
    
    TPos = [pos for pos, x in numpy.ndenumerate(array) if x == "T"]
    


Answer: This ended up being a beast to one-line, for readability it may be easier to
break it up.

    
    
    l = [['a','b','T'],
         ['T','T','g'],
         ['c','T','T']]
    
    reduce(lambda x,y: x+y, [[(x,y) for y in range(len(l[y])) if l[x][y] == 'T'] for x in range(len(l))])
    

Output

    
    
    [(0, 2), (1, 0), (1, 1), (2, 1), (2, 2)]
    

_Edit_ With `numpy` this is much simpler.

    
    
    import numpy as np
    [pos for pos, x in np.ndenumerate(np.array(l)) if x == 'T']
    

Output

    
    
    [(0, 2), (1, 0), (1, 1), (2, 1), (2, 2)]
    

