Frage

I can have the cartesian product of lists thanks to the itertools.product() function:

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

What I want is the same thing but with all different sizes:

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

I can't see the one obvious way to do it.

I would need a method that could let me set the minimum AND maximum size of the tuples (I deal with long lists and need only combinations of sizes from 7 to 3, the number of lists and their size vary).

My lists are more like:

lists = [['A', 'B', 'C'], ['1', '2'], ['x', 'y', 'z', 'u'], ...] # size may go to a few dozens
War es hilfreich?

Lösung

>>> from itertools import product, combinations
>>> lists = [['A', 'B'], ['1', '2'], ['x', 'y']]
>>> for i in xrange(2, len(lists)+1):
    for 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')]

Andere Tipps

Just chain several products together, based on combinations of smaller size:

from itertools import chain, product, combinations

def ranged_product(*lists, **start_stop):
    start, stop = start_stop.get('start', len(lists)), start_stop.get('stop', 0)
    return chain.from_iterable(product(*comb)
                               for size in xrange(start, stop - 1, -1)
                               for comb in combinations(lists, r=size))

Demo:

>>> lists = [['A', 'B'], ['1', '2'], ['x', 'y']]
>>> for prod in ranged_product(stop=2, *lists):
...     print prod
... 
('A', '1', 'x')
('A', '1', 'y')
('A', '2', 'x')
('A', '2', 'y')
('B', '1', 'x')
('B', '1', 'y')
('B', '2', 'x')
('B', '2', 'y')
('A', '1')
('A', '2')
('B', '1')
('B', '2')
('A', 'x')
('A', 'y')
('B', 'x')
('B', 'y')
('1', 'x')
('1', 'y')
('2', 'x')
('2', 'y')
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top