Python 使用itertools从列表进行组合

Python 使用itertools从列表进行组合,python,list,combinations,itertools,Python,List,Combinations,Itertools,我如何正确使用itertools中的组合函数来使用列表a一次,b两次不替换,c两次不替换,d和e各一次 import itertools a = [[2, 3], [3, 4]] b = [[5, 6], [7, 8], [9, 10]] c = [[11, 12], [13, 14]] d = [[15, 16], [17, 18]] e = [[12,16],[13,17],[14,18],[15,19]] q=[] q=list(itertools.combinations((a, b,

我如何正确使用itertools中的组合函数来使用列表a一次,b两次不替换,c两次不替换,d和e各一次

import itertools
a = [[2, 3], [3, 4]]
b = [[5, 6], [7, 8], [9, 10]]
c = [[11, 12], [13, 14]]
d = [[15, 16], [17, 18]]
e = [[12,16],[13,17],[14,18],[15,19]]

q=[]
q=list(itertools.combinations((a, b, b,c, c, d,e),7)
print q
根据预期产出的澄清更新:

:

它将在每次迭代中从每个参数中选取一个元素,使最右边的元素循环最快,最左边的元素循环最慢

在Python 3中,您可以使用扩展参数解包来更明显地表示某些参数的重复:

itertools.product(a, b, b, c, c, c, c, d, e)

或者用于在Py2上工作的更一般的序列重复。

似乎您正在寻找和的组合:使用组合获得可能的组合,而不替换重复列表,然后使用产品组合所有这些组合。您可以将列表和计数放在两个列表中,压缩这些列表,并使用生成器表达式获取所有组合

itertools.product(a, *[b]*2, *[c]*4, d, e)
在本例中,梳子生成器有48个元件,其中包括:

from itertools import product, combinations, chain
lists = [a,b,c,d,e]
counts = [1,2,2,1,1]
combs = product(*(combinations(l, c) for l, c in zip(lists, counts)))
如果你愿意,就让他们:


您试图实现的是输入项的笛卡尔积,而不是列表中项目的组合。因此,您必须使用

如果允许在多次使用的列表中重复,答案很简单:

>>> combs = (list(chain(*p)) for p in product(*(combinations(l, c) for l, c in zip(lists, counts))))
>>> list(combs)
[[[2, 3], [5, 6], [7, 8], [11, 12], [13, 14], [15, 16], [12, 16]],
 ...
 [[3, 4], [7, 8], [9, 10], [11, 12], [13, 14], [17, 18], [15, 19]]]
但是如果在同一个列表中不允许重复,它会变得有点令人讨厌,你需要将上面的答案与所提到的和结合起来。该代码将给出所有组合的列表:

但是,如果需要排列而不是组合,则必须使用以下代码更新上述代码:


b中的两个绘图是有替换还是没有替换?我不确定我是否理解您在这里想要什么。您可以给出前几个示例输出或一些需要确定的内容吗?但如果您将itertools.compositions包装在列表中以获得相当数量的输入,您几乎肯定会耗尽内存。输出的数量以大约On的速度增长!析因生长;您通常需要一个接一个地迭代组合,而不是一次存储所有组合。如果不进行替换,您可以向我们显示预期的输出吗?但这不会从a中提取一个,从b中提取两个,等等,而是从所有组合中提取9个。@tobias_k:在OP澄清确切用法之前,我回答了这个问题。Will fix。从问题上看不完全清楚,但我认为预期输出的最后一行应该包含[13,14],[11,12],而不是[11,12],[13,14]。@tobias_k查看他的样本输出,我相信他需要列表的笛卡尔积。用简单的例子更新了答案。我同意你的看法,提供的输入参数太大,不能清楚地看出他的意图。更简单的例子是有帮助的matter@WilliamBernard:如果是@tobias_k答案,则应将您标记为已接受。正如我在他之后提到的answered@WilliamBernard:未从您在问题中提供的样本中清除。所以,我提到了所有可能的解决方案:P
[(([2, 3],), ([5, 6], [7, 8]), ([11, 12], [13, 14]), ([15, 16],), ([12, 16],)),
 ...
 (([2, 3],), ([5, 6], [7, 8]), ([11, 12], [13, 14]), ([17, 18],), ([15, 19],)),
 (([2, 3],), ([5, 6], [9, 10]),([11, 12], [13, 14]), ([15, 16],), ([12, 16],)),
 ...
 (([3, 4],), ([5, 6], [7, 8]), ([11, 12], [13, 14]), ([15, 16],), ([12, 16],)),
 ...
 (([3, 4],), ([5, 6], [7, 8]), ([11, 12], [13, 14]), ([17, 18],), ([15, 19],)),
 ...
 (([3, 4],), ([7, 8], [9, 10]),([11, 12], [13, 14]), ([17, 18],), ([15, 19],))]
>>> combs = (list(chain(*p)) for p in product(*(combinations(l, c) for l, c in zip(lists, counts))))
>>> list(combs)
[[[2, 3], [5, 6], [7, 8], [11, 12], [13, 14], [15, 16], [12, 16]],
 ...
 [[3, 4], [7, 8], [9, 10], [11, 12], [13, 14], [17, 18], [15, 19]]]
>>> import itertools
>>> a = [1,2]
>>> b = [3,4]
>>> [i for i in itertools.product(a, b, b)]
[(1, 3, 3), (1, 3, 4), (1, 4, 3), (1, 4, 4), (2, 3, 3), (2, 3, 4), (2, 4, 3), (2, 4, 4)]
>>> from itertools import chain, product, combinations
>>> lists, counts = [a, b], [1, 2]  # to track that a is to be used once, and b twice
>>> list(list(chain(*p)) for p in product(*(combinations(l, c) for l, c in zip(lists, counts))))
[[1, 3, 4], [2, 3, 4]]
>>> from itertools import chain, product, permutations
>>> list(list(chain(*p)) for p in product(*(permutations(l, c) for l, c in zip(lists, counts))))
[[1, 3, 4], [1, 4, 3], [2, 3, 4], [2, 4, 3]]