Python 创建具有唯一行和列的数组
如何创建具有唯一行和列的数组,如python中的这一行和列Python 创建具有唯一行和列的数组,python,arrays,list,matrix,Python,Arrays,List,Matrix,如何创建具有唯一行和列的数组,如python中的这一行和列 [1, 2, 3, 4] [2, 3, 4, 1] [4, 1, 2, 3] [3, 4, 1, 2] 这可以通过itertools轻松实现。排列方法: import itertools a = [1,2,3,4] list(itertools.permutations(a)) [(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2,
[1, 2, 3, 4]
[2, 3, 4, 1]
[4, 1, 2, 3]
[3, 4, 1, 2]
这可以通过
itertools轻松实现。排列方法:
import itertools
a = [1,2,3,4]
list(itertools.permutations(a))
[(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4), (2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2, 4), (3, 1, 4, 2), (3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 1, 2, 3), (4, 1, 3, 2), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)]
它必须是随机的,还是每次只需将行旋转一个位置就可以了?请查看itertools中的排列函数。
import itertools
a = [1,2,3,4]
list(itertools.permutations(a))
[(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4), (2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2, 4), (3, 1, 4, 2), (3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 1, 2, 3), (4, 1, 3, 2), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)]