Reputation: 1245
I want to have a function to generate cross product of arbitrary number of arrays.
# Code to generate cross product of 3 arrays
M = [1, 1]
N = [2, 3]
K = [4, 5]
for M, N, K in itertools.product(M, N, K)
If I want to introduce a function using *, what is a good way to achieve that?
I tried the following code, but ended with an error: "TypeError: 'builtin_function_or_method' object is not iterable"
# def cross_product(*inputs):
return itertools.product(inputs)
cross_product(M, N, K)
Upvotes: 1
Views: 41
Reputation: 16623
You can actually just use unpacking without a helper function:
import itertools
L = [[1, 1],
[2, 3],
[4, 5]]
for x in itertools.product(*L):
print(x)
Upvotes: 2