Wednesday 15 June 2011

How to permutate/combinate lists of different sizes? python -



How to permutate/combinate lists of different sizes? python -

i have list of lists such, no. of inner list unknown:

>>> x = [1,2,3] >>> y = [4,5,6,7] >>> z = [8] >>> lol = [x,y,z]

i need combinations each item within inner list in order of lol lists, have been doing such desired output:

>>> in x: ... j in y: ... k in z: ... print [i,j,k] ... [1, 4, 8] [1, 5, 8] [1, 6, 8] [1, 7, 8] [2, 4, 8] [2, 5, 8] [2, 6, 8] [2, 7, 8] [3, 4, 8] [3, 5, 8] [3, 6, 8] [3, 7, 8]

what pythonic way above? there itertools function this?

i've tried itertools.product didn't desired output:

>>> itertools import product >>> in product(lol): ... print ... ([1, 2, 3],) ([4, 5, 6, 7],) ([8],)

you close:

>>> in product(*lol): ... print ... (1, 4, 8) (1, 5, 8) (1, 6, 8) (1, 7, 8) (2, 4, 8) (2, 5, 8) (2, 6, 8) (2, 7, 8) (3, 4, 8) (3, 5, 8) (3, 6, 8) (3, 7, 8)

in doc, there examples product(a, b). should pass every list argument. in case, product(x,y,z) trick. *lol notation unpacking.

python list itertools lambda

No comments:

Post a Comment