python - How to apply itertools.product to elements of a list of lists? -
I have a list of arrays and I would like to get the cartesian product of elements in the array.
I would use an example to make it more solid ...
itertools.product wants to do the trick, but I'm stuck in a little detail.
arrays = [((-1, + 1), (-2, + 2), (-3, + 3)];
If I do
cp = list (itertools.product (arrays));
get me
cp = Cp0 = [((-1, 1)), ((-2, 2)), ((-3, 3),)]
But what do I have to do? / P>
cp1 = [(-1, -2, -3), (-1, -2, +3), (-1, + 2, -3), (-1, + 2, + 3), ..., (+ 1, + 2, -3), (+ 1, 2, 3)].
I have tried a few different things:
cp = list (itertools.product (itertools.islice (arrays, lanes (arrays))) ); Cp = list (itertools.product (arrays, lanes (arrays)));
All of them gave me cp0 instead of cp1 .
Any thoughts?
Thanks in advance.
& gt; & Gt; & Gt; List (Iterols.product (* array)) [(-1, -2, -3), (-1, -2, 3), (-1, 2, -3), (-1, 2, 3) , (1, -2, -3), (1, -2, 3), (1, 2, -3), (1, 2, 3)]
Feed all the arguments in the product
as separate arguments, which will then give you the Cartesian product.
The reason your version is not working is that it is a trivial matter to ask for a Cartesian product of a list of only one argument, and give a list containing only one element product
(List provided in the list).
Comments
Post a Comment