使用二维数组中的索引从一维数组中获取值

时间:2021-02-19 16:13:20

标签: python numpy numpy-ndarray

我有一组值

values = np.array([5, 4, 3, 2, 1])

值的索引数组

indices = np.array([[2, 3],
                    [1, 2],
                    [3, 4]])

我想使用索引值作为值的索引并得到结果

np.array([[3, 2],
          [4, 3],
          [2, 1])

我该怎么做?

1 个答案:

答案 0 :(得分:3)

values[indices]
array([[3, 2],
       [4, 3],
       [2, 1]])