擅长:python、mysql、java
<p>首先应该将list<code>A</code>转换为NumPy数组:</p>
<pre><code>>>> import numpy as np
>>> A = np.array([[3, 0, 0, 8, 3],
... [9, 3, 2, 2, 6],
... [5, 5, 4, 2, 8],
... [3, 8, 7, 1, 2],
... [3, 9, 1, 5, 5]])
...
>>> y = [4, 2, 1, 3, 2]
</code></pre>
<p>在那之后,没有什么能阻止你使用<a href="http://docs.scipy.org/doc/numpy-1.10.1/reference/arrays.indexing.html" rel="noreferrer">advanced indexing</a>:</p>
<pre><code>>>> A[np.arange(A.shape[0]), y]
array([3, 2, 5, 1, 1])
>>> A[np.arange(A.shape[0]), y] = -99
>>> A
array([[ 3, 0, 0, 8, -99],
[ 9, 3, -99, 2, 6],
[ 5, -99, 4, 2, 8],
[ 3, 8, 7, -99, 2],
[ 3, 9, -99, 5, 5]])
</code></pre>