How to Apply Itertools.Product to Elements of a List of Lists

How to apply itertools.product to elements of a list of lists?

>>> list(itertools.product(*arrays))
[(-1, -2, -3), (-1, -2, 3), (-1, 2, -3), (-1, 2, 3), (1, -2, -3), (1, -2, 3), (1, 2, -3), (1, 2, 3)]

This will feed all the pairs as separate arguments to product, which will then give you the cartesian product of them.

The reason your version isn't working is that you are giving product only one argument. Asking for a cartesian product of one list is a trivial case, and returns a list containing only one element (the list given as argument).

How to get the cartesian product of a series of lists

Use itertools.product, which has been available since Python 2.6.

import itertools

somelists = [
[1, 2, 3],
['a', 'b'],
[4, 5]
]
for element in itertools.product(*somelists):
print(element)

This is the same as:

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

How to apply itertools.product to a nested list in Python

Here's what will do the trick -

import itertools
lst = [['a'], ['b', 'c'], ['d', ['e', 'f']]]
outp = list(itertools.product(*lst))
out = []
for i in outp:
temp = []
for j in i:
if isinstance(j, list):
for k in j:
temp.append(k)
else:
temp.append(j)
out.append(temp)
print(out)

Start off by forming the output materials using itertools.product and then simply formatting it in a way that the nested lists are flattened out.

Output

[['a', 'b', 'd'], ['a', 'b', 'e', 'f'], ['a', 'c', 'd'], ['a', 'c', 'e', 'f']]

Use lists inside a list with itertools.product

Unpack the lists in your list with the star operator * to apply the product to your sublists.

product=list(itertools.product(*elements))

List of lists to iterables in itertools

i think this is what you want:

import itertools

lists = [[1, 3, 4], [2, 3, 6], [9, 4]]
for i in itertools.product(*lists):
print(i)

Output:

(1, 2, 9)
(1, 2, 4)
(1, 3, 9)
(1, 3, 4)
(1, 6, 9)
(1, 6, 4)
(3, 2, 9)
(3, 2, 4)
(3, 3, 9)
(3, 3, 4)
(3, 6, 9)
(3, 6, 4)
(4, 2, 9)
(4, 2, 4)
(4, 3, 9)
(4, 3, 4)
(4, 6, 9)
(4, 6, 4)

How to call itertools.pruduct function with list of lists

Unpack the list using the * operator:

list(itertools.product(*text_input))
# [('The', 'apple', 'is', 'green'),
# ('The', 'apple', 'is', 'red'),
# ('The', 'banana', 'is', 'green'),
# ('The', 'banana', 'is', 'red')]

product of different length list using itertools in Python

Not sure if that is what you want or if it is more elegant:

from itertools import chain, product

combinations = product(
sectors,
chain.from_iterable(chain.from_iterable(rows)),
chain.from_iterable(chain.from_iterable(seats)),
)
joined_combinations = map(lambda t: "".join(t), combinations)
list(joined_combinations)
# returns
['A1a', 'A1b', 'A1a', 'A1b', 'A1c', 'A1d', 'A1a', ...]

Explanation: Applying two times chain.from_iterable you can "unpack" individual characters from the nested lists, then creating the product of the items of the unnested lists (which creates 3-tuples) and finally join the items of each 3-tuple together.

If you want to avoid duplicates you can put a set() around each argument in the product.

Itertools product of list

I think that perhaps you want to get all tuples consisting of 1 item from each column and each row, as in a determinant calculation. If so:

from itertools import permutations

def afficherListe(A):
"""A is a square matrix. Returns all tuples used in det(A)"""
n = len(A)
return [tuple(A[i][j] for i,j in enumerate(p)) for p in permutations(range(n))]

#tests:
A = [[1,2],[3,4]]
B = [[1,2,3],[4,5,6],[7,8,9]]
print(afficherListe(A))
print(afficherListe(B))

Output:

[(1, 4), (2, 3)]
[(1, 5, 9), (1, 6, 8), (2, 4, 9), (2, 6, 7), (3, 4, 8), (3, 5, 7)]


Related Topics



Leave a reply



Submit