Reputation: 6768
I have multiple array like following
A[0] = [a1, a2, a3]
A[1] = [b1, b2]
...
A[k-1] = [k1, ...]
I would like to generate all the vector [a?, b?, ..., k?]
possible
I looked at itertool library, but it does not feet my need.
How can I simply do it. (I though about recursion but I guess there is a more simpler solution).
Upvotes: 0
Views: 51
Reputation: 1121834
You'd use itertools.product()
:
from itertools import product
for vector in product(*A):
# vector is [a[0], b[0], ..., k[0]] through to [a[k-1], [b[k-1], ..., k[k-1]]
# odometer style
Demo:
>>> from itertools import product
>>> A = [[1, 2, 3], ['foo', 'bar'], ['spam', 'eggs']]
>>> for vector in product(*A):
... print vector
...
(1, 'foo', 'spam')
(1, 'foo', 'eggs')
(1, 'bar', 'spam')
(1, 'bar', 'eggs')
(2, 'foo', 'spam')
(2, 'foo', 'eggs')
(2, 'bar', 'spam')
(2, 'bar', 'eggs')
(3, 'foo', 'spam')
(3, 'foo', 'eggs')
(3, 'bar', 'spam')
(3, 'bar', 'eggs')
Upvotes: 4