且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过指定行和列从另一个数组创建NumPy数组

更新时间:2022-04-19 22:53:50

做到这一点的***方法是使用ix_函数:请参见

The best way to do this is to use the ix_ function: see the answer by MSeifert for details.

或者,您可以通过xy使用链式索引操作:

Alternatively, you could use chain the indexing operations using x and y:

>>> A[x][:,y]
array([[ 2,  4,  5],
       [12, 14, 15]])

第一个x用于选择A的行.接下来,[:,y]选择由y的元素指定的子数组的列.

First x is used to select the rows of A. Next, [:,y] picks out the columns of the subarray specified by the elements of y.

在这种情况下,链接是对称的:如果愿意,还可以先使用A[:,y][x]选择列.

The chaining is symmetric in this case: you can also choose the columns first with A[:,y][x] if you wish.