擅长:python、mysql、java
<p>这对派对来说有点晚了,但默认的Numpy方式是<a href="http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.take.html" rel="nofollow noreferrer">^{<cd1>}</a>。但是,这个<em>总是</em>复制数据(因为它支持奇特的索引,所以它总是假设这是可能的)。为了避免这种情况(在许多情况下,您需要的是数据的<em>视图</em>,而不是副本),请回退到另一个答案中已经提到的<code>slice(None)</code>选项,可能将其包装成一个很好的函数:</p>
<pre><code>def simple_slice(arr, inds, axis):
# this does the same as np.take() except only supports simple slicing, not
# advanced indexing, and thus is much faster
sl = [slice(None)] * arr.ndim
sl[axis] = inds
return arr[tuple(sl)]
</code></pre>