# 获得一系列列表的笛卡尔积？

input：

``somelists = [ [1, 2, 3], ['a', 'b'], [4, 5] ]` `

` `[(1, 'a', 4), (1, 'a', 5), (1, 'b', 4), (1, 'b', 5), (2, 'a', 4), (2, 'a', 5) ...]` `

` `import itertools for element in itertools.product(*somelists): print(element)` `

` `import itertools >>> for i in itertools.product([1,2,3],['a','b'],[4,5]): ... print i ... (1, 'a', 4) (1, 'a', 5) (1, 'b', 4) (1, 'b', 5) (2, 'a', 4) (2, 'a', 5) (2, 'b', 4) (2, 'b', 5) (3, 'a', 4) (3, 'a', 5) (3, 'b', 4) (3, 'b', 5) >>>` `

` `>>> [(a, b, c) for a in [1,2,3] for b in ['a','b'] for c in [4,5]] [(1, 'a', 4), (1, 'a', 5), (1, 'b', 4), (1, 'b', 5), (2, 'a', 4), (2, 'a', 5), (2, 'b', 4), (2, 'b', 5), (3, 'a', 4), (3, 'a', 5), (3, 'b', 4), (3, 'b', 5)]` `

` `def product(*args): if not args: return iter(((),)) # yield tuple() return (items + (item,) for items in product(*args[:-1]) for item in args[-1])` `

` `>>> list(product([1,2,3], ['a','b'], [4,5])) [(1, 'a', 4), (1, 'a', 5), (1, 'b', 4), (1, 'b', 5), (2, 'a', 4), (2, 'a', 5), (2, 'b', 4), (2, 'b', 5), (3, 'a', 4), (3, 'a', 5), (3, 'b', 4), (3, 'b', 5)] >>> list(product([1,2,3])) [(1,), (2,), (3,)] >>> list(product([])) [] >>> list(product()) [()]` `

` `import itertools result = list(itertools.product(*somelists))` `

` `def product(*args, **kwds): # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111 pools = map(tuple, args) * kwds.get('repeat', 1) result = [[]] for pool in pools: result = [x+[y] for x in result for y in pool] for prod in result: yield tuple(prod)` `

` `def product(ar_list): if not ar_list: yield () else: for a in ar_list[0]: for prod in product(ar_list[1:]): yield (a,)+prod print list(product([[1,2],[3,4],[5,6]]))` `

` `[(1, 3, 5), (1, 3, 6), (1, 4, 5), (1, 4, 6), (2, 3, 5), (2, 3, 6), (2, 4, 5), (2, 4, 6)]` `

` `somelists = [ [1, 2, 3], ['a', 'b'], [4, 5] ] cart_prod = [(a,b,c) for a in somelists[0] for b in somelists[1] for c in somelists[2]]` `

` `import itertools import sympy x, y = sympy.symbols('x y') somelist = [[x,y], [1,2,3], [4,5]] somelist2 = [[1,2], [1,2,3], [4,5]] for element in itertools.product(*somelist): print element` `

## 迭代方法

` `def cartesian_iterative(pools): result = [[]] for pool in pools: result = [x+[y] for x in result for y in pool] return result` `

## recursion方法

` `def cartesian_recursive(pools): if len(pools) > 2: pools[0] = product(pools[0], pools[1]) del pools[1] return cartesian_recursive(pools) else: pools[0] = product(pools[0], pools[1]) del pools[1] return pools def product(x, y): return [xx + [yy] if isinstance(xx, list) else [xx] + [yy] for xx in x for yy in y]` `

## Lambda方法

` `def cartesian_reduct(pools): return reduce(lambda x,y: product(x,y) , pools)` `

` `def product_args(*args): if args: for a in args[0]: for prod in product(*args[1:]) if its[1:] else ((),): yield (a,) + prod` `

` `def product2(ar_list): """ >>> list(product(())) [()] >>> list(product2(())) [] """ return product_args(*ar_list)` `