Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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 如何列出所有可能的排列?_Python_List_Permutation - Fatal编程技术网

Python 如何列出所有可能的排列?

Python 如何列出所有可能的排列?,python,list,permutation,Python,List,Permutation,是否有一个函数可以生成列表列表的所有排列,同时保持它们的顺序 例如: func([[1, 2, 3], [10, 20, 30]]) 产生: [[1, 10], [1, 20], [1, 30], [2, 10], [2, 20], [2, 30], [3, 10], [3, 20], [3, 30]] itertools模块中有一个产品函数,它的作用正好是: from itertools import product lst = [[1, 2, 3], [10, 20, 30]] res

是否有一个函数可以生成列表列表的所有排列,同时保持它们的顺序

例如:

func([[1, 2, 3], [10, 20, 30]])
产生:

[[1, 10], [1, 20], [1, 30], [2, 10], [2, 20], [2, 30], [3, 10], [3, 20], [3, 30]]

itertools模块中有一个
产品
函数,它的作用正好是:

from itertools import product

lst = [[1, 2, 3], [10, 20, 30]]
res = [list(i) for i in product(*lst)]
print(res)
因为您希望内部项也是列表,所以我使用
list(I)
product
提供的元组转换为列表

输出:


[[1,10]、[1,20]、[1,30]、[2,10]、[2,20]、[2,30]、[3,10]、[3,20]、[3,30]。

到目前为止,您在搜索此类函数时发现了什么?您已经在Python标准库中看到了
itertools
模块了吗?顺便说一句,您所展示的示例称为“笛卡尔积”,而不是“置换”。您正在寻找
itertools.product
,而不是
permutations