简体   繁体   English

结合两个列表和 output 列表 1 给定列表 2 的所有可能组合

[英]Combine two list and output all possible combinations of list 1 given list 2

I have two lists in python.我在 python 中有两个列表。

list1 = ['A', 'B']
list2 = [True, False]

list1 has been simplified in the real world it will be more than 2 elements. list1 在现实世界中已被简化,它将超过 2 个元素。 list 2 will only be true or false.清单 2 只会是真或假。

I'd like to generate the following output, given 2 elements in list1:我想生成以下 output,给定 list1 中的 2 个元素:

output=[
    [['A', True], ['B', True]],
    [['A', False], ['B', True]],
    [['A', True], ['B', False]],
    [['A', False], ['B', False]]]

I'd like the algorithm to be able to support the scenario with more than 2 elements in list1.我希望算法能够支持 list1 中包含 2 个以上元素的场景。

Here is an example with 3 elements in list1:以下是 list1 中包含 3 个元素的示例:

list1 = ['A', 'B', 'C']
list2 = [True, False]

output=[
    [['A', True], ['B', True], ['C', True]],
    [['A', True], ['B', True], ['C', False]],
    [['A', True], ['B', False], ['C', True]],
    [['A', False], ['B', True], ['C', True]],
    [['A', False, ['B', False], ['C', True]],
    [['A', True], ['B', False, ['C', False]],
    [['A', False], ['B', True], ['C', False]],
    [['A', False], ['B', False], ['C', False]]
    ]

The following gets me close but not quite there.以下让我接近但不完全在那里。

[zip(x, list2) for x in it.permutations(list1, len(list2))]

I know I need to do something using itertools but cannot wrap my head around it.我知道我需要使用 itertools 做一些事情,但无法绕开它。 Any advice would be greatly appreciated.任何建议将不胜感激。

from pprint import pprint
import itertools

list1 = ['A', 'B']
list2 = [True, False]

newdict = {}
final_list = []
for element in itertools.product(list1,list2):
    if element[0] not in newdict.keys():
        newdict[element[0]] = []
    newdict[element[0]].append(list(element))
values =  (list(newdict.values()))


for x in itertools.product('01', repeat=len(values)):
    tmp_list = []
    for i,index in enumerate(list(x)):
        tmp_list.append(values[int(i)][int(index)])
    final_list.append(tmp_list)

pprint (final_list)

OUTPUT: OUTPUT:

[[['A', True], ['B', True]],
 [['A', True], ['B', False]],
 [['A', False], ['B', True]],
 [['A', False], ['B', False]]]

OUTPUT WITH A,B,C: OUTPUT 带 A、B、C:

[[['A', True], ['B', True], ['C', True]],
 [['A', True], ['B', True], ['C', False]],
 [['A', True], ['B', False], ['C', True]],
 [['A', True], ['B', False], ['C', False]],
 [['A', False], ['B', True], ['C', True]],
 [['A', False], ['B', True], ['C', False]],
 [['A', False], ['B', False], ['C', True]],
 [['A', False], ['B', False], ['C', False]]]

Initially I take the cartesian product, but I separate each key (A,B, or C) into its own list.最初我采用笛卡尔积,但我将每个键(A、B 或 C)分隔到自己的列表中。 That yields:这会产生:

[[['A', True], ['A', False]],
 [['B', True], ['B', False]],
 [['C', True], ['C', False]]]

From there we are just counting in binary to index into those list.从那里我们只是以二进制计数以索引到这些列表中。 eg例如

000 = A,True B,True C,True
001 = A,True B,True C,False
...
111 = A,False B,False C,False

You are nearly there with your list comprehension.您的列表理解力几乎就在那里。 You just need to use product instead of permutations , since you want to repeat the same values from list2 .您只需要使用product而不是permutations ,因为您想从list2重复相同的值。 Try this:尝试这个:

output = [list(zip(list1, x)) for x in itertools.product(list2, repeat=len(list1))]

This creates a list of lists of 2-tuples.这将创建一个 2 元组列表的列表。 If you need the innermost elements to be lists too, you can use list(map(list, zip(...))) .如果您也需要最里面的元素是列表,则可以使用list(map(list, zip(...)))

I think this would work我认为这会奏效

import itertools
list1=['A','B']
list2 = [True, False]

output=[]
for a,b in itertools.product(list1,list2):
    temp=[]
    temp.append(a)
    temp.append(b)
    output.append(temp)

print(output)

It is basically permuting everything in list1 with list2它基本上是用 list2 置换 list1 中的所有内容

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

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