I couldn't get a good title, but what I want to do is simple.
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