Get distance to non-empty item in two-dimensional list column
<p>I am trying to make a game of scrabble. I have a board, defined below.</p>

<pre><code>self.board = [[" ", "A ", "B ", "C ", "D ", "E ", "F ", "G ", "H ", "I ", "J ", "K ", "L ", "M ", "N ", "O "],
    ['01', 'TWS', ' ', ' ', 'DLS', ' ', ' ', ' ', 'TWS', ' ', ' ', ' ', 'DLS', ' ', ' ', 'TWS'],
    ['02', ' ', 'DWS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'DWS', ' '],
    ['03', ' ', ' ', 'DWS', ' ', ' ', ' ', 'DLS', ' ', 'DLS', ' ', ' ', ' ', 'DWS', ' ', ' '],
    ['04', 'DLS', ' ', ' ', 'DWS', ' ', ' ', ' ', 'DLS', ' ', ' ', ' ', 'DWS', ' ', ' ', 'DLS'],
    ['05', ' ', ' ', ' ', ' ', 'DWS', ' ', ' ', ' ', ' ', ' ', 'DWS', ' ', ' ', ' ', ' '],
    ['06', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' '],
    ['07', ' ', ' ', 'DLS', ' ', ' ', ' ', 'DLS', ' ', 'DLS', ' ', ' ', ' ', 'DLS', ' ', ' '],
    ['08', 'TWS', ' ', ' ', 'DLS', ' ', ' ', ' ', 'B', 'O', 'G', ' ', 'DLS', ' ', ' ', 'TWS'],
    ['09', ' ', ' ', 'DLS', ' ', ' ', ' ', 'DLS', ' ', 'DLS', ' ', ' ', ' ', 'DLS', ' ', ' '],
    ['10', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' '],
    ['11', ' ', ' ', ' ', ' ', 'DWS', ' ', ' ', ' ', ' ', ' ', 'DWS', ' ', ' ', ' ', ' '],
    ['12', 'DLS', ' ', ' ', 'DWS', ' ', ' ', ' ', 'DLS', ' ', ' ', ' ', 'DWS', ' ', ' ', 'DLS'],
    ['13', ' ', ' ', 'DWS', ' ', ' ', ' ', 'DLS', ' ', 'DLS', ' ', ' ', ' ', 'DWS', ' ', ' '],
    ['14', ' ', 'DWS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'TLS', ' ', ' ', ' ', 'DWS', ' '],
    ['15', 'TWS', ' ', ' ', 'DLS', ' ', ' ', ' ', 'TWS', ' ', ' ', ' ', 'DLS', ' ', ' ', 'TWS']]        
</code></pre>

<p>My goal is to find the distance to the nearest letter from any letter. For example, if I called the function on B, it would return</p>

<pre><code>{"up" : 7, "down" : 7, "left" : 7, "right" : 0}
</code></pre>

<p>I have experimented with the built-in <code>next</code> function, but I guess my question is, is there an easy way to get the column of a two-dimensional list?</p>

<p>I also have a list of things that should be considered as empty:</p>

<pre><code>emptyList = "TWS", "DWS", "TLS", "DLS"
</code></pre>

<p>Please help. Thank you so much!</p>

<p>You could use <code>next</code>, and extract the column with <code>[row[col_num] for row in board]</code> like this:</p>

<pre><code>def distances(row_num, col_num):
    letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    if not col_num.isdigit():
        col_num = ord(col_num.upper()) - ord('A') + 1
    col = [row[col_num] for row in board]
    row = board[row_num]
    return {
        'right': next((i for i, c in enumerate(row[col_num+1:]) if c in letters), 15-col_num),
        'left': next((i for i, c in enumerate(row[col_num-1:0:-1]) if c in letters), col_num-1),
        'down': next((i for i, c in enumerate(col[row_num+1:]) if c in letters), 15-row_num),
        'up': next((i for i, c in enumerate(col[row_num-1:0:-1]) if c in letters), row_num-1)
    }

print (distances(8, 'H'))
</code></pre>

<p>The arguments to the function should be the row number (<code>8</code>) and column number (<code>8</code>) or the corresponding letter <code>H</code>.</p>

<p>To check that a square is empty the function checks whether the contents are not a single letter (A-Z).</p>

<p>See it run on <a href="https://repl.it/Dx2V" rel="nofollow">repl.it</a></p>

python python-3.x multidimensional-array distance