如何在python中获得不同的组合?

时间:2015-03-20 06:42:36

标签: python python-2.7

鉴于Lista每个都包含一个ID名称(例如a),所以 符号'='和逗号分隔的值列表。我需要生成另一个列表,它是'id= value'子串的组合,这样每个id-value组合都存在于输入中,每个值只使用一次。

Lista:
    [
    'a= aVal1,aVal2',
    'b=bVal1,bVal2,bVal3',
    'c= cVal1,cVal2',
    ]

预期产出:

Listb:
    [
    'a=aVal1& b=bVal1&c=cVal1',
    'a=aVal1&b=tyVal1&c=cVal2',
    'a=aVal1&b=tyVal2&c=tzVal1',
    'a=aVal1&b=tyVal2&c=tzVal2',
    ]

我尝试使用itertools.permutations()函数解决此问题,但我无法继续进行。我应该采取什么样的方法来解决这个问题?

3 个答案:

答案 0 :(得分:4)

我通常不会发布完整的工作解决方案,看起来像家庭作业问题,所以认为自己很幸运。 :)

#!/usr/bin/env python

from itertools import product

lista = [
    'a=aVal1,aVal2',
    'b=bVal1,bVal2,bVal3',
    'c=cVal1,cVal2',
]

newlists = []
for s in lista:
    head, _, tail = s.partition('=')
    newlists.append(['%s=%s' % (head, u) for u in tail.split(',')])

listb = ['&'.join(t) for t in product(*newlists)]

for row in listb:
    print row

<强>输出

a=aVal1&b=bVal1&c=cVal1
a=aVal1&b=bVal1&c=cVal2
a=aVal1&b=bVal2&c=cVal1
a=aVal1&b=bVal2&c=cVal2
a=aVal1&b=bVal3&c=cVal1
a=aVal1&b=bVal3&c=cVal2
a=aVal2&b=bVal1&c=cVal1
a=aVal2&b=bVal1&c=cVal2
a=aVal2&b=bVal2&c=cVal1
a=aVal2&b=bVal2&c=cVal2
a=aVal2&b=bVal3&c=cVal1
a=aVal2&b=bVal3&c=cVal2

我已将列表的名称更改为小写 - 以大写字母开头的名称(如ListaListb通常保留用作Python中的类名。

如果您不理解我在此计划中所做的任何事情,请询问,我会尝试解释。


可以在一行中执行此操作,但这并不容易阅读:

listb = ['&'.join(t)for t in product(*[['%s=%s'%(i,v)for v in j.split(',')]for i,j in[s.split('=')for s in lista]])]

:)


字符串.partition() method将字符串拆分为3个部分:分隔符之前的子字符串,分隔符本身以及分隔符之后的子字符串。
因此,head, _, tail = s.partition('=')会将ID标记为head,并将所有相关值标记为tail 我使用_捕获分隔符以指示我们确实不需要该值(因为我们已经知道它是=)。
下一行的列表推导使用.split(',')将值拆分。

答案 1 :(得分:1)

>>> l=[i.split('=') for i in a]
>>> g=[[i]+j.split(',') for i,j in l]
>>> ['&'.join(('='.join(m),'='.join(k),'='.join(t))) for m,k,t in (product(*[list(combinations(i,2)) for i in g],repeat=1))]
['a= aVal1&b=bVal1&c= cVal1', 'a= aVal1&b=bVal1&c=cVal2', 'a= aVal1&b=bVal1& cVal1=cVal2', 'a= aVal1&b=bVal2&c= cVal1', 'a= aVal1&b=bVal2&c=cVal2', 'a= aVal1&b=bVal2& cVal1=cVal2', 'a= aVal1&b=bVal3&c= cVal1', 'a= aVal1&b=bVal3&c=cVal2', 'a= aVal1&b=bVal3& cVal1=cVal2', 'a= aVal1&bVal1=bVal2&c= cVal1', 'a= aVal1&bVal1=bVal2&c=cVal2', 'a= aVal1&bVal1=bVal2& cVal1=cVal2', 'a= aVal1&bVal1=bVal3&c= cVal1', 'a= aVal1&bVal1=bVal3&c=cVal2', 'a= aVal1&bVal1=bVal3& cVal1=cVal2', 'a= aVal1&bVal2=bVal3&c= cVal1', 'a= aVal1&bVal2=bVal3&c=cVal2', 'a= aVal1&bVal2=bVal3& cVal1=cVal2', 'a=aVal2&b=bVal1&c= cVal1', 'a=aVal2&b=bVal1&c=cVal2', 'a=aVal2&b=bVal1& cVal1=cVal2', 'a=aVal2&b=bVal2&c= cVal1', 'a=aVal2&b=bVal2&c=cVal2', 'a=aVal2&b=bVal2& cVal1=cVal2', 'a=aVal2&b=bVal3&c= cVal1', 'a=aVal2&b=bVal3&c=cVal2', 'a=aVal2&b=bVal3& cVal1=cVal2', 'a=aVal2&bVal1=bVal2&c= cVal1', 'a=aVal2&bVal1=bVal2&c=cVal2', 'a=aVal2&bVal1=bVal2& cVal1=cVal2', 'a=aVal2&bVal1=bVal3&c= cVal1', 'a=aVal2&bVal1=bVal3&c=cVal2', 'a=aVal2&bVal1=bVal3& cVal1=cVal2', 'a=aVal2&bVal2=bVal3&c= cVal1', 'a=aVal2&bVal2=bVal3&c=cVal2', 'a=aVal2&bVal2=bVal3& cVal1=cVal2', ' aVal1=aVal2&b=bVal1&c= cVal1', ' aVal1=aVal2&b=bVal1&c=cVal2', ' aVal1=aVal2&b=bVal1& cVal1=cVal2', ' aVal1=aVal2&b=bVal2&c= cVal1', ' aVal1=aVal2&b=bVal2&c=cVal2', ' aVal1=aVal2&b=bVal2& cVal1=cVal2', ' aVal1=aVal2&b=bVal3&c= cVal1', ' aVal1=aVal2&b=bVal3&c=cVal2', ' aVal1=aVal2&b=bVal3& cVal1=cVal2', ' aVal1=aVal2&bVal1=bVal2&c= cVal1', ' aVal1=aVal2&bVal1=bVal2&c=cVal2', ' aVal1=aVal2&bVal1=bVal2& cVal1=cVal2', ' aVal1=aVal2&bVal1=bVal3&c= cVal1', ' aVal1=aVal2&bVal1=bVal3&c=cVal2', ' aVal1=aVal2&bVal1=bVal3& cVal1=cVal2', ' aVal1=aVal2&bVal2=bVal3&c= cVal1', ' aVal1=aVal2&bVal2=bVal3&c=cVal2', ' aVal1=aVal2&bVal2=bVal3& cVal1=cVal2']

解释:

首先,您需要将字符串列表与=分开,然后创建g,如下所示:

>>> g
[['a', ' aVal1', 'aVal2'], ['b', 'bVal1', 'bVal2', 'bVal3'], ['c', ' cVal1', 'cVal2']]

现在您需要创建列表g的元素与len 2的组合,之后您需要production该结果!注意我们需要从以下结果列表中拾取一个元组:

>>> [list(combinations(i,2)) for i in g]
[[('a', ' aVal1'), ('a', 'aVal2'), (' aVal1', 'aVal2')], [('b', 'bVal1'), ('b', 'bVal2'), ('b', 'bVal3'), ('bVal1', 'bVal2'), ('bVal1', 'bVal3'), ('bVal2', 'bVal3')], [('c', ' cVal1'), ('c', 'cVal2'), (' cVal1', 'cVal2')]]

答案 2 :(得分:0)

您应该查看itertools.product,这可能就是您要找的内容。

您可以轻松地将listA转换为3个列表:

a = ['aVal1', 'aVal2']
b = ['bVal1', 'bVal2', 'bVal3']
c = ['cVal1', 'cVal2']

然后你可以尝试

for x in itertools.product(a, b, c):
   print x

x将是您想要的,所有剩余的工作都是字符串连接或字符串格式化。

<强>更新

有关详细信息,请使用此

all_the_list = []
listB = []
for i in listA:
    name, values = i.split('=');
    k = values.strip().split(',');
    all_the_list.append(k)

// then product the out put
for aV, bV, cV in itertools.product(*all_the_list):
    listB.append('a=%s&b=%s&c=%s'%(aV, bV, cV))

希望这个帮助