python - Cartesian product of different size -


i can have cartesian product of lists itertools.product() function :

lists = [['a', 'b'], ['1', '2'], ['x', 'y']] combinations = itertools.product(*lists) # [('a', '1', 'x'), ('a', '2', 'y'), ..., ('b', '2', 'y')] 

what want same thing different sizes :

all_comb = magicfunction(lists) # [('a', '1', 'x'), ..., ('b', '2', 'y'), ('a', '1'), ('a', '2'), ... ('2', 'y'), ... ('y')] 

i can't see 1 obvious way it.

i need method let me set minimum , maximum size of tuples (i deal long lists , need combinations of sizes 7 3, number of lists , size vary).

my lists more :

lists = [['a', 'b', 'c'], ['1', '2'], ['x', 'y', 'z', 'u'], ...] # size may go few dozens 

>>> itertools import product, combinations >>> lists = [['a', 'b'], ['1', '2'], ['x', 'y']] >>> in xrange(2, len(lists)+1):     c in combinations(lists, i):         print list(product(*c)) ...          [('a', '1'), ('a', '2'), ('b', '1'), ('b', '2')] [('a', 'x'), ('a', 'y'), ('b', 'x'), ('b', 'y')] [('1', 'x'), ('1', 'y'), ('2', 'x'), ('2', 'y')] [('a', '1', 'x'), ('a', '1', 'y'), ('a', '2', 'x'), ('a', '2', 'y'), ('b', '1', 'x'), ('b', '1', 'y'), ('b', '2', 'x'), ('b', '2', 'y')] 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -