组合列表保存列表顺序以获得链

时间:2020-04-14 21:10:44

标签: python

我有一个字符串列表的列表:

answers = [['blue', 'green', 'orange'], ['yes', 'no'], ['saturday', 'sunday', 'weekend', 'none of them', 'continue'] ... ]

这是一个测验,我想算出所有答案的组合。

我知道我可以按照以下步骤为每个答案找出不同的组合

from itertools import combinations

q1 = list(combinations(answers[0], 1))
q2 = list(combinations(answers[1], 1))
q3 = list(combinations(answers[2], 3))

然后,我将不得不对具有多个答案的问题进行列表理解,但是答案noneither是排他的。

但是我不清楚如何将这些单独的组合组合成更大的链,从而保留顺序并仍然给出所有答案链。

这是硒项目的一部分,在其中单击答案列表中的每个项目,因此保留变量名很重要。

从示例答案中,链将为:

chain 1 = [['blue'], ['yes'], ['saturday', 'continue']]
chain 2 = [['blue'], ['yes'], ['saturday', 'sunday', 'continue']]
chain 3 = [['blue'], ['yes'], ['saturday', 'sunday', 'weekend', 'continue']]
chain 4 = [['blue'], ['yes'], ['saturday', 'weekend', 'continue']]
chain 5 = [['blue'], ['yes'], ['weekend', 'sunday', 'continue']]
chain 6 = [['blue'], ['yes'], ['sunday', 'continue']]
chain 7 = [['blue'], ['yes'], ['weekend', 'continue']]
chain 8 = [['blue'], ['yes'], ['continue']]
chain 9 = [['blue'], ['yes'], ['none of them']]
chain 10 = [['blue'], ['no'], ['saturday', 'continue']]
chain 11 = [['blue'], ['no'], ['saturday', 'sunday', 'continue']]
chain 12 = [['blue'], ['no'], ['saturday', 'sunday', 'weekend', 'continue']]
chain 13 = [['blue'], ['no'], ['saturday', 'weekend', 'continue']]
chain 14 = [['blue'], ['no'], ['weekend', 'sunday', 'continue']]
chain 15 = [['blue'], ['no'], ['sunday', 'continue']]
chain 16 = [['blue'], ['no'], ['weekend', 'continue']]
chain 17 = [['blue'], ['no'], ['continue']]
chain 18 = [['blue'], ['no'], ['none of them']]
...

2 个答案:

答案 0 :(得分:2)

您实际上是在做一组嵌套的循环,每个问题一个,以创建所有可能的组合:

for a1 in combinations(lst[0], 1):
    for a2 in combinations(lst[1], 1):
        for a3 in combinations(lst[2], 3):
            print(a1, a2, a3)

那会很快变得笨拙。您可以使用itertools.product同时遍历所有循环来展平循环。


from itertools import combinations, product

q1 = combinations(lst[0], 1)
q2 = combinations(lst[1], 1)
q3 = combinations(lst[2], 3)

for a1, a2, a3 in product(q1, q2, q3):
    print(a1, a2, a3)

答案 1 :(得分:1)

如果我没记错的话,您不仅是在通过已知组合进行列表理解吗?即

[(q1_item, q2_item, q3_item) for q1_item in q1 for q2_item in q2 for q3_item in q3]

或完整示例:

>>> from itertools import combinations
>>> answers = [['blue', 'green', 'orange'], ['yes', 'no'], ['saturday', 'sunday', 'weekend', 'none of them', 'continue']]
>>> q1 = list(combinations(answers[0], 1))
>>> q1
[('blue',), ('green',), ('orange',)]
>>> q2 = list(combinations(answers[1], 1))
>>> q2
[('yes',), ('no',)]
>>> q3 = list(combinations(answers[2], 3))
>>> q3
[('saturday', 'sunday', 'weekend'), ('saturday', 'sunday', 'none of them'), ('saturday', 'sunday', 'continue'), ('saturday', 'weekend', 'none of them'), ('saturday', 'weekend', 'continue'), ('saturday', 'none of them', 'continue'), ('sunday', 'weekend', 'none of them'), ('sunday', 'weekend', 'continue'), ('sunday', 'none of them', 'continue'), ('weekend', 'none of them', 'continue')]
>>> [(q1_item, q2_item, q3_item) for q1_item in q1 for q2_item in q2 for q3_item in q3]
[(('blue',), ('yes',), ('saturday', 'sunday', 'weekend')), (('blue',), ('yes',), ('saturday', 'sunday', 'none of them')), (('blue',), ('yes',), ('saturday', 'sunday', 'continue')), (('blue',), ('yes',), ('saturday', 'weekend', 'none of them')), (('blue',), ('yes',), ('saturday', 'weekend', 'continue')), (('blue',), ('yes',), ('saturday', 'none of them', 'continue')), (('blue',), ('yes',), ('sunday', 'weekend', 'none of them')), (('blue',), ('yes',), ('sunday', 'weekend', 'continue')), (('blue',), ('yes',), ('sunday', 'none of them', 'continue')), (('blue',), ('yes',), ('weekend', 'none of them', 'continue')), (('blue',), ('no',), ('saturday', 'sunday', 'weekend')), (('blue',), ('no',), ('saturday', 'sunday', 'none of them')), (('blue',), ('no',), ('saturday', 'sunday', 'continue')), (('blue',), ('no',), ('saturday', 'weekend', 'none of them')), (('blue',), ('no',), ('saturday', 'weekend', 'continue')), (('blue',), ('no',), ('saturday', 'none of them', 'continue')), (('blue',), ('no',), ('sunday', 'weekend', 'none of them')), (('blue',), ('no',), ('sunday', 'weekend', 'continue')), (('blue',), ('no',), ('sunday', 'none of them', 'continue')), (('blue',), ('no',), ('weekend', 'none of them', 'continue')), (('green',), ('yes',), ('saturday', 'sunday', 'weekend')), (('green',), ('yes',), ('saturday', 'sunday', 'none of them')), (('green',), ('yes',), ('saturday', 'sunday', 'continue')), (('green',), ('yes',), ('saturday', 'weekend', 'none of them')), (('green',), ('yes',), ('saturday', 'weekend', 'continue')), (('green',), ('yes',), ('saturday', 'none of them', 'continue')), (('green',), ('yes',), ('sunday', 'weekend', 'none of them')), (('green',), ('yes',), ('sunday', 'weekend', 'continue')), (('green',), ('yes',), ('sunday', 'none of them', 'continue')), (('green',), ('yes',), ('weekend', 'none of them', 'continue')), (('green',), ('no',), ('saturday', 'sunday', 'weekend')), (('green',), ('no',), ('saturday', 'sunday', 'none of them')), (('green',), ('no',), ('saturday', 'sunday', 'continue')), (('green',), ('no',), ('saturday', 'weekend', 'none of them')), (('green',), ('no',), ('saturday', 'weekend', 'continue')), (('green',), ('no',), ('saturday', 'none of them', 'continue')), (('green',), ('no',), ('sunday', 'weekend', 'none of them')), (('green',), ('no',), ('sunday', 'weekend', 'continue')), (('green',), ('no',), ('sunday', 'none of them', 'continue')), (('green',), ('no',), ('weekend', 'none of them', 'continue')), (('orange',), ('yes',), ('saturday', 'sunday', 'weekend')), (('orange',), ('yes',), ('saturday', 'sunday', 'none of them')), (('orange',), ('yes',), ('saturday', 'sunday', 'continue')), (('orange',), ('yes',), ('saturday', 'weekend', 'none of them')), (('orange',), ('yes',), ('saturday', 'weekend', 'continue')), (('orange',), ('yes',), ('saturday', 'none of them', 'continue')), (('orange',), ('yes',), ('sunday', 'weekend', 'none of them')), (('orange',), ('yes',), ('sunday', 'weekend', 'continue')), (('orange',), ('yes',), ('sunday', 'none of them', 'continue')), (('orange',), ('yes',), ('weekend', 'none of them', 'continue')), (('orange',), ('no',), ('saturday', 'sunday', 'weekend')), (('orange',), ('no',), ('saturday', 'sunday', 'none of them')), (('orange',), ('no',), ('saturday', 'sunday', 'continue')), (('orange',), ('no',), ('saturday', 'weekend', 'none of them')), (('orange',), ('no',), ('saturday', 'weekend', 'continue')), (('orange',), ('no',), ('saturday', 'none of them', 'continue')), (('orange',), ('no',), ('sunday', 'weekend', 'none of them')), (('orange',), ('no',), ('sunday', 'weekend', 'continue')), (('orange',), ('no',), ('sunday', 'none of them', 'continue')), (('orange',), ('no',), ('weekend', 'none of them', 'continue'))]

您还可以添加其他条件来排除某些组合。

相关问题