我想使用列表和np.array
索引来索引一个np.array,但它们会得到不同的结果。
以下是一个示例:
import numpy as np
x = np.arange(10)
idx = [[0, 1], [1, 2]]
x[np.array(idx)] # returns array([[0, 1], [1, 2]])
但是直接应用列表会出错
x[idx] # raises IndexError: too many indices for array
我期望上述代码返回与使用
np.array
索引相同的结果。有任何想法吗?我正在使用
Python 3.5
和NumPy 1.13.1
。
x[tuple(idx)]
还是x[np.array(idx)]
。 - Eric