【发布时间】:2018-09-17 01:03:45
【问题描述】:
假设我有一个 3x3 numpy 数组:
[[0, 1, 2],
[2, 0, 1],
[1, 2, 0]]
还有一个额外的向量:
[2,
1,
1]
对于矩阵中的每一行,我想将第 0 个索引处的数字与向量中相应索引处的数字交换。在本例中,交换后的最终输出为:
[[2, 1, 0], # the 0th and 2nd positions have been swapped
[0, 2, 1], # the 0th and 1st positions have been swapped
[2, 1, 0]] # the 0th and 1st positions have been swapped
你怎么能做到这一点?
【问题讨论】:
-
x[[0, 1, 2], [2, 1, 1]], x[[0, 1, 2], [0, 0, 0]] = x[[0, 1, 2], [0, 0, 0]], x[[0, 1, 2], [2, 1, 1]]
标签: python arrays numpy matrix