Python 如何获得两个列表的乘积?
在python上,我可以使用Python 如何获得两个列表的乘积?,python,Python,在python上,我可以使用itertools中的combinations函数来获取列表中所有可能的成对项。但是有没有一种方法可以得到所有可能的组合,一个元素在一个列表中,另一个元素在另一个列表中 l1 = [1,2,3] l2 = [4,5] 是否有返回的函数 (1,4),(1,5),(2,4),(2,5),(3,4),(3,5) 您正在寻找“笛卡尔积”: 例子 from itertools import product l1 = [1,2,3] l2 = [4,5] >>
itertools
中的combinations
函数来获取列表中所有可能的成对项。但是有没有一种方法可以得到所有可能的组合,一个元素在一个列表中,另一个元素在另一个列表中
l1 = [1,2,3]
l2 = [4,5]
是否有返回的函数
(1,4),(1,5),(2,4),(2,5),(3,4),(3,5)
您正在寻找“笛卡尔积”:
例子
from itertools import product
l1 = [1,2,3]
l2 = [4,5]
>>> print list(product(l1, l2))
[(1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)]