[PYTHON] Obtain the direct product of sequences of indefinite length within a specific sequence.

I couldn't get a good title, but what I want to do is simple.

Thing you want to do

I want to get the direct product of arrays with different lengths and different lengths!

A = [[1,2],[3,4]]
f(A)

↑ This is the output of this ↓

(1, 3)
(1, 4)
(2, 3)
(2, 4)
B = [[1],[2,3],[4,5,6]]
f(B)

↑ This is the output of this ↓

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

unpack! It seems that you can loosen the array or dictionary by adding *.

A = [1,2]
print(*A) #1 2 

In other words! You can write like this ↓

import itertools

def f(X):
    for x in itertools.product(*X):
        print(x)

A = [[1,2],[3,4]]
B = [[1],[2,3],[4,5,6]]

f(A)
f(B)

It's OK!

Recommended Posts

Obtain the direct product of sequences of indefinite length within a specific sequence.
Calculate the product of matrices with a character expression?
Obtain the sequence information of the translated protein from the mutation information of CDS
Get the number of specific elements in a python list
Display the signal strength RSSI of a specific SSID (mac)
pandas Fetch the name of a column that contains a specific character