Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_Itertools - Fatal编程技术网

在Python中创建两组列表的所有组合

在Python中创建两组列表的所有组合,python,list,itertools,Python,List,Itertools,我尝试使用以下方法创建两组列表的所有组合: x = [[1,2,3],[4,5,6]] y = [['a','b','c'],['d','e','f']] combos = [[1,2,3,'a','b','c'],[4,5,6,'d','e','f'],[4,5,6,'a','b','c'],[4,5,6,'d','e','f']] 我认为itertools可能会有所帮助,但不确定如何帮助。谢谢您可以使用产品和链: from itertools import product, chain

我尝试使用以下方法创建两组列表的所有组合:

x = [[1,2,3],[4,5,6]]
y = [['a','b','c'],['d','e','f']]

combos  = [[1,2,3,'a','b','c'],[4,5,6,'d','e','f'],[4,5,6,'a','b','c'],[4,5,6,'d','e','f']]

我认为itertools可能会有所帮助,但不确定如何帮助。谢谢

您可以使用
产品

from itertools import product, chain
[list(chain(*i)) for i in product(x, y)]

#[[1, 2, 3, 'a', 'b', 'c'],
# [1, 2, 3, 'd', 'e', 'f'],
# [4, 5, 6, 'a', 'b', 'c'],
# [4, 5, 6, 'd', 'e', 'f']]

或者您可以使用列表:

[i + j for i in x for j in y]

#[[1, 2, 3, 'a', 'b', 'c'],
# [1, 2, 3, 'd', 'e', 'f'],
# [4, 5, 6, 'a', 'b', 'c'],
# [4, 5, 6, 'd', 'e', 'f']]

您可以使用
产品

from itertools import product, chain
[list(chain(*i)) for i in product(x, y)]

#[[1, 2, 3, 'a', 'b', 'c'],
# [1, 2, 3, 'd', 'e', 'f'],
# [4, 5, 6, 'a', 'b', 'c'],
# [4, 5, 6, 'd', 'e', 'f']]

或者您可以使用列表:

[i + j for i in x for j in y]

#[[1, 2, 3, 'a', 'b', 'c'],
# [1, 2, 3, 'd', 'e', 'f'],
# [4, 5, 6, 'a', 'b', 'c'],
# [4, 5, 6, 'd', 'e', 'f']]