简体   繁体   English

两个列表的组合(不是元素)

[英]Combinations of two lists (not element-wise)

I have two lists: 我有两个清单:

a = ['a', 'b']
b = [1, 2, 3]

I want to get the combinations produced between the elements of list b and the elements of list a but treating elements of a as pairs (or triples etc. etc.) as the example below which gives len(b) ** len(a) number of combinations. 我想获得列表的元素之间产生的组合b和列表中的元素,但治疗的元件a作为对(或三元组等等,等等),如下这给示例len(b) ** len(a)组合数量。

c = ["a_1 b_1", "a_1 b_2", "a_1 b_3", "a_2 b_1", "a_2 b_2", "a_2 b_3", "a_3 b_1", "a_3 b_2" "a_3 b_3"]

I have tried to use itertools.product (as described here ) but this will give only the 6 possible combinations. 我曾尝试使用itertools.product (如描述在这里 ),但这将只给出6种可能的组合。

You can use itertools.product(..) but specify repeat to be repeat=len(a) . 您可以使用itertools.product(..)但指定repeatrepeat=len(a) So you can use: 所以你可以使用:

from itertools import product

def mul_product(a,b):
    for tup in product(b,repeat=len(a)):
        yield ' '.join('%s_%s'%t for t in zip(a,tup))

The product(..) will generate tuples like: product(..)将生成如下元组:

>>> list(product(b,repeat=len(a)))
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]

So here the first element of the tuple is the one that is attached to a_ , the second one to b_ . 所以这里元组的第一个元素是附加到a_元素,第二个元素附加到b_ Now we zip(..) them together with the a list, producing: 现在我们zip(..)他们连同a清单,生产:

>>> list(map(lambda bi:list(zip(a,bi)),product(b,repeat=len(a))))
[[('a', 1), ('b', 1)], [('a', 1), ('b', 2)], [('a', 1), ('b', 3)], [('a', 2), ('b', 1)], [('a', 2), ('b', 2)], [('a', 2), ('b', 3)], [('a', 3), ('b', 1)], [('a', 3), ('b', 2)], [('a', 3), ('b', 3)]]

Now it is only a matter of formatting ( '%s_%s'%t ), and ' '.join(..) ining them together and yield them (or you can use list comprehension to produce a list). 现在只需格式化( '%s_%s'%t )和' '.join(..)它们放在一起并yield它们(或者你可以使用列表理解来生成列表)。

The result for your sample input is: 您的示例输入的结果是:

>>> list(mul_product(a,b))
['a_1 b_1', 'a_1 b_2', 'a_1 b_3', 'a_2 b_1', 'a_2 b_2', 'a_2 b_3', 'a_3 b_1', 'a_3 b_2', 'a_3 b_3']

Note that the elements here are generated lazily. 请注意,这里的元素是懒惰生成的。 This can be useful if you are for instance only interested in the first k ones, or when you do not want to generate all of them at once. 如果您仅对前k个感兴趣,或者您不想一次生成所有这些内容,则此功能非常有用。

You could explicitly create your pairwise items using itertools.product , then operate on those pairs again with itertools.product 你可以通过明确创建您的配对项目itertools.product ,然后对那些与重新操作itertools.product

import itertools
a = ['a', 'b']
b = [1, 2, 3]
pairs = [list(itertools.product([ai], b)) for ai in a]

pairs will contain the two lists that can fed into itertools.product again. pairs将包含可以再次输入itertools.product的两个列表。

list(itertools.product(*pairs))

The result is: 结果是:

[(('a', 1), ('b', 1)),
 (('a', 1), ('b', 2)),
 (('a', 1), ('b', 3)),
 (('a', 2), ('b', 1)),
 (('a', 2), ('b', 2)),
 (('a', 2), ('b', 3)),
 (('a', 3), ('b', 1)),
 (('a', 3), ('b', 2)),
 (('a', 3), ('b', 3))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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