是否有一个算法来找到2列出了独特的组合? 5名单? [英] Is there an algorithm to find unique combinations of 2 lists? 5 lists?

查看:121
本文介绍了是否有一个算法来找到2列出了独特的组合? 5名单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 N 列表我想找到的独特组合。我已经写出来我的白板,这一切似乎都一个模式,我只是还没有找到它。我觉得我可以EX preSS蛮力的方法,这将肯定是一些我的追求。是否有别的选择吗?将不同的数据结构(二叉树?)做这样的工作比较合适?

由于

 #1 2
一个= [1,2]
B = [A,B]
 

其结果将是:

  C = [1A,1B,2A,2B]#(4个独特的组合)
 

由于

  V = [1,一]
瓦特= [1,b]的
X = [1,C]
Y = [1,D]
Z = [1,E]
 

其结果将是:

  R = [11111,1bcde,11cde,111de,1111e,a1111,ab111,abc11,ABCD1,ABCDE,1b1d1,1bc1e,11c11,11c1e,...]
 

解决方案

也许你正在寻找itertools.product:

 #!的/ usr /斌/包膜蟒蛇
进口itertools
一个= [1,2]
B = ['一','B']
C = [在itertools.product海峡(多个)+ STR(吨)为S,T(A,B)]
打印(C)
['1a中','1B','2a中','图2b']

V = [1,'一个']
瓦特= [1,'B']
X = [1,'C']
Y = [1,'D']
Z = [1,'E']

R = [''。加入([海峡(ELT)为ELT中P])对于p在itertools.product(V,W,X,Y,Z)]
打印(R)
#['11111','1111e','111d1','111de','11c11','11c1e','11cd1','11cde','1b111','1b11e','1b1d1','1b1de', '1bc11','1bc1e','1bcd1','1bcde','a1111','a111e','a11d1','a11de','a1c11','a1c1e','a1cd1','a1cde','ab111 ','ab11e','ab1d1','ab1de','abc11','abc1e','ABCD1','ABCDE']
 

请注意该产品的收益率2 ** 5元。这是你想要的吗?

itertools.product是在Python 2.6。对于previous版本,你可以使用这样的:

 高清产品(*的args,** kwds):
        '''
        资料来源:http://docs.python.org/library/itertools.html#itertools.product
        '''
        #产品('ABCD','XY') - >斧好哦Bx的通过CX赛扬霉素镝
        #产物(范围(2),重复= 3) - > 000 001 010 011 100 101 110 111
        池=图(元组,参数)* kwds.get('重复',1)
        结果= [[]]
        在池池:
            结果= [X +的结果Y]对于x y的池]
        为督促的结果:
            产量元组(PROD)
 

编辑:软糖指出,原题要求独特的套。上述code不会产生独特的设置是否 A B v 是W X 以Z 包含重复的元素。如果这是你的问题,那么你就可以在每个列表发送到itertools.product之前转换为一组:

  R = [''。加入([STR(ELT)的英语教学中的P])对p的itertools.product(*(套(ELT)的ELT(Ⅴ, W,X,Y,Z)))]
 

I have N Lists I'd like to find unique combinations of. I've written it out on my whiteboard and it all seems to have a pattern, I just haven't found it yet. I feel I can express a brute-force method and that will certainly be something I pursue. Is there an alternative? Would a different data structure (binary tree?) make a job like this more appropriate?

Given:

#    1  2
a = [1, 2]
b = [a, b]

The result would be:

c = [1a, 1b, 2a, 2b] # (4 unique combinations)

Given:

v = [1, a]
w = [1, b]
x = [1, c]
y = [1, d]
z = [1, e]

The result would be:

r = [11111, 1bcde, 11cde, 111de, 1111e, a1111, ab111, abc11, abcd1, abcde, 1b1d1, 1bc1e, 11c11, 11c1e, ... ]

解决方案

Perhaps you are looking for itertools.product:

#!/usr/bin/env python
import itertools
a=[1,2]
b=['a','b']
c=[str(s)+str(t) for s,t in itertools.product(a,b)]
print(c)
['1a', '1b', '2a', '2b']

v=[1,'a']
w=[1,'b']
x=[1,'c']
y=[1,'d']
z=[1,'e']

r=[''.join([str(elt) for elt in p]) for p in itertools.product(v,w,x,y,z)]
print(r)
# ['11111', '1111e', '111d1', '111de', '11c11', '11c1e', '11cd1', '11cde', '1b111', '1b11e', '1b1d1', '1b1de', '1bc11', '1bc1e', '1bcd1', '1bcde', 'a1111', 'a111e', 'a11d1', 'a11de', 'a1c11', 'a1c1e', 'a1cd1', 'a1cde', 'ab111', 'ab11e', 'ab1d1', 'ab1de', 'abc11', 'abc1e', 'abcd1', 'abcde']

Note that product yields 2**5 elements. Is this what you want?

itertools.product is in Python 2.6. For previous versions, you can use this:

def product(*args, **kwds):
        '''
        Source: http://docs.python.org/library/itertools.html#itertools.product
        '''
        # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
        # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
        pools = map(tuple, args) * kwds.get('repeat', 1)
        result = [[]]
        for pool in pools:
            result = [x+[y] for x in result for y in pool]
        for prod in result:
            yield tuple(prod)

Edit: As jellybean points out, the original question asks for unique sets. The above code will not produce unique sets if a,b,v,w,x,y, or z contain repeated elements. If this is an issue for you, then you can convert each list to a set before sending it to itertools.product:

r=[''.join([str(elt) for elt in p]) for p in itertools.product(*(set(elt) for elt in (v,w,x,y,z)))]

这篇关于是否有一个算法来找到2列出了独特的组合? 5名单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆