如何获取Python中列表的1到n个列表的所有组合

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

我有一个列表列表,例如:[[a,b,c],[1,2,3],[x,y]]

我想制作[a],[b],...,[a,1],[a,2],...,[a,1,x],[a,1,y]

通过解决方案,我看到了 itertools.combinations 如何生成单个列表的所有组合,并且 itertools.product 可以生成最高级别的组合,即上面示例中的 3 个元素

我不确定如何在不分解列表结构列表的情况下完成 1 到 n 列表的所有组合,并使用 itertools.combinations 和一些布尔检查来确保我没有组合来自同一列表的元素。

我认为这就是您要查找的内容:

>>> 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')]

您只需对列表列表的所有前缀取 itertools.product(即 x[:1]x[:2]、...)。外部列表理解只是为了展平由内部列表理解生成的列表列表。

您可以在列表理解中使用 itertools.combinationsitertools.product() 来计算所有单个、对和三元组的乘积:

>>> 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')]]]

之前的帖子提供了涉及嵌套推导的简明解决方案,但缺少可能的子列表集的几个产品,如 ('a', 'x')。我将尝试以更易读的方式对其进行分解:

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')]