【发布时间】:2020-12-06 10:39:15
【问题描述】:
有一个数组:
[[1,2],
[3,4]]
如何生成所有行排列的数组列表? 我希望得到如下结果:
[[[1,2],
[3,4]],
[[1,2],
[4,3]],
[[2,1],
[4,3]],
[[2,1],
[3,4]]]
重要的是第一行的值不要与第二行或任何其他行的值混合。
【问题讨论】:
标签: arrays python-3.x numpy combinations
有一个数组:
[[1,2],
[3,4]]
如何生成所有行排列的数组列表? 我希望得到如下结果:
[[[1,2],
[3,4]],
[[1,2],
[4,3]],
[[2,1],
[4,3]],
[[2,1],
[3,4]]]
重要的是第一行的值不要与第二行或任何其他行的值混合。
【问题讨论】:
标签: arrays python-3.x numpy combinations
以下适用于 n 行的唯一区别是您获得了一个元组列表:
from itertools import product, permutations
[*product(*[permutations(row) for row in matrix])]
# For no duplicates:
[*product(*[set(permutations(row)) for row in matrix])]
【讨论】:
np.array([*product(*[permutations(row) for row in matrix])]) 也可以)