简体   繁体   中英

How to get all combinations of 1 to n lists of lists in Python

I have a list of lists say for example: [[a,b,c],[1,2,3],[x,y]]

I would want to produce [a],[b],...,[a,1],[a,2],...,[a,1,x],[a,1,y]

Looking through solutions, I've seen how itertools.combinations can produce all combinations of an individual list and itertools.product can produce the highest level of combinations ie 3 elements in the example above

I'm not sure how to go through all of the combinations of 1 to n lists without breaking down the list of list structure and using itertools.combinations with some Boolean check to make sure I'm not combining elements from the same list.

I take it this is what you're looking for:

>>> import itertools
>>> x = [['a', 'b', 'c'], [1, 2, 3], ['x', 'y']] # your list of lists
>>> [tup for sublist in [itertools.product(*x[:n+1]) for n in range(len(x))] 
         for tup in sublist]
[('a',), ('b',), ('c',), 
 ('a', 1), ('a', 2), ('a', 3), 
 ('b', 1), ('b', 2), ('b', 3), 
 ('c', 1), ('c', 2), ('c', 3), 
 ('a', 1, 'x'), ('a', 1, 'y'), ('a', 2, 'x'), ('a', 2, 'y'), ('a', 3, 'x'), ('a', 3, 'y'), 
 ('b', 1, 'x'), ('b', 1, 'y'), ('b', 2, 'x'), ('b', 2, 'y'), ('b', 3, 'x'), ('b', 3, 'y'), 
 ('c', 1, 'x'), ('c', 1, 'y'), ('c', 2, 'x'), ('c', 2, 'y'), ('c', 3, 'x'), ('c', 3, 'y')]

You just need to take itertools.product over all prefixes of your list of lists (ie x[:1] , x[:2] , ...). The outer list comprehension is just for flattening the list-of-lists generated by the inner list comprehension.

The previous posts have offered concise solutions involving nested comprehensions, but are missing several products of possible sets of sublists like ('a', 'x') . I will try to break it down in a more readable way:

lst = [['a', 'b', 'c'], [1, 2, 3], ['x', 'y']]
result = []  # collect your products

# n sublists: iterate over all 'sub_lengthes'
for length in xrange(1, len(lst)+1):
    # iterate over all possible combinations of sublists
    for c in itertools.combinations(lst, length):
        # iterate over all products for each combination
        for res in itertools.product(*c):
            result.append(res)

print(result)

>>> result
# 3 + 3 + 2 = 8 singletons 
[('a',), ('b',), ('c',), (1,), (2,), (3,), ('x',), ('y',), 
# 3*3 + 3*2 + 3*2 = 21 pairs
('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 1), ('c', 2), ('c', 3), 
('a', 'x'), ('a', 'y'), ('b', 'x'), ('b', 'y'), ('c', 'x'), ('c', 'y'),
(1, 'x'), (1, 'y'), (2, 'x'), (2, 'y'), (3, 'x'), (3, 'y'), 
# 3*3*2 = 18 triplets
('a', 1, 'x'), ('a', 1, 'y'), ('a', 2, 'x'), ('a', 2, 'y'), ('a', 3, 'x'), ('a', 3, 'y'), ('b', 1, 'x'), ('b', 1, 'y'), ('b', 2, 'x'), ('b', 2, 'y'), ('b', 3, 'x'), ('b', 3, 'y'), ('c', 1, 'x'), ('c', 1, 'y'), ('c', 2, 'x'), ('c', 2, 'y'), ('c', 3, 'x'), ('c', 3, 'y')]

You can use itertools.combinations and itertools.product() within a list comprehension to calculate the product of all single, pair and triples :

>>> from itertools import product
>>> lst = [['a','b','c'],[1,2,3],['x','y']]
>>> [[list(product(*t)) for t in combinations(lst,i)] for i in range(1,len(lst)+1)]
[[[('a',), ('b',), ('c',)], [(1,), (2,), (3,)], [('x',), ('y',)]], 
 [[('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('b', 3), ('c', 1), ('c', 2), ('c', 3)], [('a', 'x'), ('a', 'y'), ('b', 'x'), ('b', 'y'), ('c', 'x'), ('c', 'y')], [(1, 'x'), (1, 'y'), (2, 'x'), (2, 'y'), (3, 'x'), (3, 'y')]],
[[('a', 1, 'x'), ('a', 1, 'y'), ('a', 2, 'x'), ('a', 2, 'y'), ('a', 3, 'x'), ('a', 3, 'y'), ('b', 1, 'x'), ('b', 1, 'y'), ('b', 2, 'x'), ('b', 2, 'y'), ('b', 3, 'x'), ('b', 3, 'y'), ('c', 1, 'x'), ('c', 1, 'y'), ('c', 2, 'x'), ('c', 2, 'y'), ('c', 3, 'x'), ('c', 3, 'y')]]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM