Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从n个整数列表(长度可能不等)中进行所有可能的n长度置换_Python_Algorithm_Combinations_Permutation_Itertools - Fatal编程技术网

Python 从n个整数列表(长度可能不等)中进行所有可能的n长度置换

Python 从n个整数列表(长度可能不等)中进行所有可能的n长度置换,python,algorithm,combinations,permutation,itertools,Python,Algorithm,Combinations,Permutation,Itertools,例如: list1 = [0,1,2] list2 = [0,1] list3 = [0,1,2,3] 那么排列将是: 0,0,0 0,0,1 0,0,2 0,0,3 0,1,0 0,1,1 0,1,2 0,1,3 1,0,0 1,0,1 1,0,2 1,0,3 1,1,0 1,1,1 1,1,2 1,1,3 …以此类推,3 x 2 x 4=24个排列 列表的数量不一定是3(它们可以是任意数量,n),并且顺序很重要,因此0,0,1与0,1,0不同 我知道我可能不得不以某种方式使用iterto

例如:

list1 = [0,1,2]
list2 = [0,1]
list3 = [0,1,2,3]
那么排列将是:

0,0,0
0,0,1
0,0,2
0,0,3
0,1,0
0,1,1
0,1,2
0,1,3
1,0,0
1,0,1
1,0,2
1,0,3
1,1,0
1,1,1
1,1,2
1,1,3
…以此类推,3 x 2 x 4=24个排列

列表的数量不一定是3(它们可以是任意数量,n),并且顺序很重要,因此0,0,1与0,1,0不同

我知道我可能不得不以某种方式使用itertools,但不确定如何实现这一点。我不能只做三个嵌套循环,因为列表的数量不同

这是的变体,但列表的数量不同,顺序也不同


我感谢任何帮助或暗示。谢谢。

你没有找到一个好的副本,因为你把“排列”和“笛卡尔积”混淆了。谢谢,这确实是笛卡尔积,这正是我想要的。我把它错误地命名为“排列”,我的错。@user2898278它不是“你的错”-别担心。有时只是不知道发生了什么事。你在研究中表现出了努力,并展示了输入/输出示例——这是一个好问题。
list1 = [0,1,2]
list2 = [0,1]
list3 = [0,1,2,3]
listOfLists = [list1,list2,list3]
for list in itertools.product(*listOfLists):
    print(list)