两个列表之间的组合?

已经有一段时间了,我无法绕过我试图制定的一个algorithm。 基本上,我有两个列表,并希望得到这两个列表的所有组合。

我可能不会解释它是正确的,所以这里是一个例子。

name = 'a', 'b' number = 1, 2 

在这种情况下的输出将是:

 1. A1 B2 2. B1 A2 

棘手的部分是我可能比“数字”variables中的项目“名称”variables更多的项目(数字将始终等于或小于名称variables)。

我很困惑如何做所有的组合(嵌套for循环?),甚至更多的逻辑混淆,如果名称中的项目多于数字列表中的项目,则将名称variables中的项目移位。

我不是最好的程序员,但是如果有人能够帮助我澄清逻辑/algorithm来实现这一点,我想我可以试试看。 所以我刚被嵌套for循环。

更新:

这里有3个variables和2个数字的输出:

 name = 'a', 'b', 'c' number = 1, 2 

输出:

 1. A1 B2 2. B1 A2 3. A1 C2 4. C1 A2 5. B1 C2 6. C1 B2 

假设len(list1) >= len(list2) 。 那么你想要的是从列表list1取出长度为len(list2)所有排列,并将它们与列表2中的项目进行匹配。 在python中:

 >>> import itertools >>> list1=['a','b','c'] >>> list2=[1,2] >>> [zip(x,list2) for x in itertools.permutations(list1,len(list2))] [[('a', 1), ('b', 2)], [('a', 1), ('c', 2)], [('b', 1), ('a', 2)], [('b', 1), ('c', 2)], [('c', 1), ('a', 2)], [('c', 1), ('b', 2)]] 

最简单的方法是使用itertools.product

 a = ["foo", "melon"] b = [True, False] c = list(itertools.product(a, b)) >> [("foo", True), ("foo", False), ("melon", True), ("melon", False)] 

可能比上面最简单的简单一些:

 a = ["foo", "bar"] b = [1, 2, 3] [(x,y) for x in a for y in b] >>>[('foo', 1), ('foo', 2), ('foo', 3), ('bar', 1), ('bar', 2), ('bar', 3)] 

没有任何import

从interjay的答案微小的改进,使结果作为一个平坦的名单。

 >>> list3 = [zip(x,list2) for x in itertools.permutations(list1,len(list2))] >>> import itertools >>> chain = itertools.chain(*list3) >>> list4 = list(chain) [('a', 1), ('b', 2), ('a', 1), ('c', 2), ('b', 1), ('a', 2), ('b', 1), ('c', 2), ('c', 1), ('a', 2), ('c', 1), ('b', 2)] 

从这个链接引用

没有itertools

 [(list1[i], list2[j]) for i in xrange(len(list1)) for j in xrange(len(list2))] 

我正在寻找一个乘以自己的列表,只有唯一的组合,这是作为这个function提供。

 import itertools itertools.combinations(list, n_times) 

这里作为itertools上Python文档的一个摘录,这可能会帮助你find你想要的东西。

 Combinatoric generators: Iterator | Results -----------------------------------------+---------------------------------------- product(p, q, ... [repeat=1]) | cartesian product, equivalent to a | nested for-loop -----------------------------------------+---------------------------------------- permutations(p[, r]) | r-length tuples, all possible | orderings, no repeated elements -----------------------------------------+---------------------------------------- combinations(p, r) | r-length tuples, in sorted order, no | repeated elements -----------------------------------------+---------------------------------------- combinations_with_replacement(p, r) | r-length tuples, in sorted order, | with repeated elements -----------------------------------------+---------------------------------------- product('ABCD', repeat=2) | AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD permutations('ABCD', 2) | AB AC AD BA BC BD CA CB CD DA DB DC combinations('ABCD', 2) | AB AC AD BC BD CD combinations_with_replacement('ABCD', 2) | AA AB AC AD BB BC BD CC CD DD 

你可能想尝试一个单行列表理解:

 >>> [name+number for name in 'ab' for number in '12'] ['a1', 'a2', 'b1', 'b2'] >>> [name+number for name in 'abc' for number in '12'] ['a1', 'a2', 'b1', 'b2', 'c1', 'c2']