Python如何从列表中获取笛卡尔积
1、可以使用itertools.product在标准库中使用以获取笛卡尔积。...= [
[1, 2, 3],
['a', 'b'],
[4, 5]
]
result = list(product(*somelists))
print(result)
2、迭代方法... = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
return result
3、递归方法..., y):
return [xx + [yy] if isinstance(xx, list) else [xx] + [yy] for xx in x for yy in y]
4、Lambda方法...def cartesian_reduct(pools):
return reduce(lambda x,y: product(x,y) , pools)
以上就是Python从列表中获取笛卡尔积的方法