Python合并具有所有可能排列的两个列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32438350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:34:09  来源:igfitidea点击:

Python merging two lists with all possible permutations

pythonlistitertools

提问by GeoJunkie

I'm trying to figure out the best way to merge two lists into all possible combinations. So, if I start with two lists like this:

我试图找出将两个列表合并为所有可能组合的最佳方法。所以,如果我从两个这样的列表开始:

list1 = [1, 2]
list2 = [3, 4]

The resulting list will look like this:

结果列表将如下所示:

[[[1,3], [2,4]], [[1,4], [2,3]]]

That is, it basically produces a list of lists, with all the potential combinations between the two.

也就是说,它基本上会生成一个列表列表,其中包含两者之间的所有潜在组合。

I've been working through itertools, which I'm pretty sure holds the answer, but I can't come up with a way to make it act this way. The closest I came was:

我一直在研究 itertools,我很确定它有答案,但我想不出办法让它以这种方式运行。我最接近的是:

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
print list(itertools.product(list1, list2))

Which produced:

其中产生:

[(1, 5), (1, 6), (1, 7), (1, 8), (2, 5), (2, 6), (2, 7), (2, 8), (3, 5), (3, 6), (3, 7), (3, 8), (4, 5), (4, 6), (4, 7), (4, 8)]

So it does all the possible combinations of items in each list, but not all the possible resulting lists. How do I get that to happen?

所以它会处理每个列表中所有可能的项目组合,但不是所有可能的结果列表。我怎样才能做到这一点?

EDIT: The end goal is to be able to individually process each list to determine efficiency (the actual data I'm working with is more complex). So, in the original example above, it would work something like this:

编辑:最终目标是能够单独处理每个列表以确定效率(我正在使用的实际数据更复杂)。因此,在上面的原始示例中,它的工作方式如下:

list1 = [1, 2]
list2 = [3, 4]

Get first merged list: [[1,3], [2, 4]]
    Do stuff with this list
Get second merged list: [[1,4], [2, 3]]
    Do stuff with this list

If I got the "list of lists of lists" output I described above, then I could put it into a for loop and process on. Other forms of output would work, but it seems the simplest to work with.

如果我得到了上面描述的“列表列表”输出,那么我可以将它放入一个 for 循环并继续处理。其他形式的输出也可以,但它似乎最容易使用。

采纳答案by pacholik

repeatthe first list, permutatethe second and zipit all together

repeat第一个列表,permutate第二个列表和zip它们一起

>>> from itertools import permutations, repeat
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> list(list(zip(r, p)) for (r, p) in zip(repeat(a), permutations(b)))
[[(1, 4), (2, 5), (3, 6)],
 [(1, 4), (2, 6), (3, 5)],
 [(1, 5), (2, 4), (3, 6)],
 [(1, 5), (2, 6), (3, 4)],
 [(1, 6), (2, 4), (3, 5)],
 [(1, 6), (2, 5), (3, 4)]]

EDIT: As Peter Otten noted, the inner zipand the repeatare superfluous.

编辑:正如 Peter Otten 所指出的,内在ziprepeat是多余的。

[list(zip(a, p)) for p in permutations(b)]

回答by Joe Smart

Edited my code to give you your desired output.

编辑我的代码,为您提供所需的输出。

list1 = [1,2]
list2 = [3,4]
combined = []

for a in list1:
    new_list = []
    for b in list2:
        new_list.append([a, b])
    combined.append(new_list)

print combined

回答by Semih Yagcioglu

You can create a list by constructing all the permutations of two list members with this, containing the list combinations.

您可以通过使用此构造两个列表成员的所有排列来创建列表,其中包含列表组合。

lst1 = [1,2]
lst2 = [3,4]

#lst = [[j,k] for j in lst1 for k in lst2] # [[1,3],[1,4],[2,3],[2,4]]
lst = [[[j,k] for j in lst1] for k in lst2] # [[[1,3],[2,3]],[[1,4],[2,4]]]
print lst

回答by pydude

Try this:

尝试这个:

combos=[]
for i in list1:
      for j in list2:
          combos.append([i,j])
print combos

回答by Sdwdaw

Try to use list generator to create the nested lists:

尝试使用列表生成器来创建嵌套列表:

>>> [[[x,y] for x in list1] for y in list2]
[[[1, 3], [2, 3]], [[1, 4], [2, 4]]]
>>>

Or, if you want one-line list, just delete brackets:

或者,如果您想要单行列表,只需删除括号:

>>> [[x,y] for x in list1 for y in list2]
[[1, 3], [1, 4], [2, 3], [2, 4]]

回答by Peter Otten

The accepted answer can be simplified to

接受的答案可以简化为

a = [1, 2, 3]
b = [4, 5, 6]
[list(zip(a, p)) for p in permutations(b)]

(The list() call can be omitted in Python 2)

(在 Python 2 中可以省略 list() 调用)

回答by Minato

As @pacholik s answer does not cover lists of different length, here is my solution, using a list comprehension with two variables:

由于@pacholik 的回答不包括不同长度的列表,这是我的解决方案,使用带有两个变量的列表理解:

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

combinations = [(a,b) for a in first_list for b in second_list]

The output looks like this:

输出如下所示:

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