import numpy as np
x = np.array([[1,2 ,3], [9,8,7]])
y = np.array([[2,1 ,0], [1,0,2]])
x[y]
预期输出:
array([[3,2,1], [8,9,7]])
如果 x 和 y 是一维数组,则 x[y] 可以。那么对二维数组执行此操作的 numpy 方式或最 pythonic 或有效的方式是什么?
最佳答案
您需要定义相应的行索引。
一种方法是:
>>> x[np.arange(x.shape[0])[..., None], y]
array([[3, 2, 1],
[8, 9, 7]])
关于python - 基于 2D numpy 索引数组排列 numpy 2D 数组的 numpy 方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30274838/