Index a 2D Numpy array with 2 lists of indices
<p>I've got a strange situation.  </p>

<p>I have a 2D Numpy array, x:</p>

<pre><code>x = np.random.random_integers(0,5,(20,8))
</code></pre>

<p>And I have 2 indexers--one with indices for the rows, and one with indices for the column.  In order to index X, I am having to do the following:</p>

<pre><code>row_indices = [4,2,18,16,7,19,4]
col_indices = [1,2]
x_rows = x[row_indices,:]
x_indexed = x_rows[:,column_indices]
</code></pre>

<p>Instead of just: </p>

<pre><code>x_new = x[row_indices,column_indices]
</code></pre>

<p>(which fails with: error, cannot broadcast (20,) with (2,))</p>

<hr>

<p>I'd like to be able to do the indexing in one line using the broadcasting, since that would keep the code clean and readable...also, I don't know all that much about python under the hood, but as I understand it, it should be faster to do it in one line (and I'll be working with pretty big arrays). </p>

<hr>

<p>Test Case:</p>

<pre><code>x = np.random.random_integers(0,5,(20,8))

row_indices = [4,2,18,16,7,19,4]
col_indices = [1,2]
x_rows = x[row_indices,:]
x_indexed = x_rows[:,col_indices]

x_doesnt_work = x[row_indices,col_indices]
</code></pre>