Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据三个列表生成所有可能的元组_Python_List_Python 2.7_Combinations_Itertools - Fatal编程技术网

Python 根据三个列表生成所有可能的元组

Python 根据三个列表生成所有可能的元组,python,list,python-2.7,combinations,itertools,Python,List,Python 2.7,Combinations,Itertools,有趣的答案,请允许我修改问题。 在代码上做了一些更改后,我得到了以下结果: #coding:utf-8 import itertools stuff = [1, 2, 3, 4, 5, 8, 10, 13, 16, 17, 18, 20, 21, 22, 25] for L in range(5, 6): for subset in itertools.combinations(stuff, L): subset = list(subset) sub

有趣的答案,请允许我修改问题。

在代码上做了一些更改后,我得到了以下结果:

#coding:utf-8

import itertools


stuff = [1, 2, 3, 4, 5, 8, 10, 13, 16, 17, 18, 20, 21, 22, 25]
for L in range(5, 6):
    for subset in itertools.combinations(stuff, L):
        subset = list(subset)
        subset.extend([7, 9, 11, 15, 19, 23, 6, 12, 14, 24])
        print(subset)
它的输出是这样的:

[1, 2, 3, 4, 5, 7, 9, 11, 15, 19, 23, 6, 12, 14, 24]
[1, 2, 3, 4, 8, 7, 9, 11, 15, 19, 23, 6, 12, 14, 24]
...
它产生大约3000条生产线

它对列表中的五个数字进行了所有可能的组合,并将另一个列表(subset.extend([7,9,11,15,19,23,6,12,14,24])添加到每个组合(子集)中。这似乎是对的,我不确定

但我真正想让它做的是:

1-输入三个列表(配对和取消配对)

2-然后,程序将从7个成对数字中生成4个数字的所有组合,并对取消配对进行相同操作,从8个取消配对数字中生成4个数字的所有组合,并将其绑定在一起,生成一个列表,其中包含8个所有可能的组合,其中4个数字来自配对,4个数字来自unpair,如:

[2, 12, 20, 10, 1, 5, 19, 23]
[2, 12, 20, 10, 5, 19, 25, 13]
...
3-对于生成的pair和unpair组合中的每一行,它将使用列表中的7个数字组合来完成,生成一个包含15个数字的列表,而不重复类似的数字

[2, 12, 20, 10, 1, 5, 19, 23, 25, 3, 4, 8, 17, 21, 22]
[2, 12, 20, 10, 5, 19, 25, 13, 3, 4, 8, 17, 21, 22, 11]
...

这就是我被卡住的地方。如何为每个列表生成组合并绑定它们生成15个数字的列表,而无需重复数字和序列。

您可以在for循环中手动编写该逻辑:

for combo in itertools.combinations(stuff, 15):
    if set(combo).issuperset(pair) and set(combo).issuperset(unpair):
        print(combo)

注意:此特定代码仅在“stuff”没有重复项时有效

您的意思还不完全清楚。你能给出一些示例输出吗?这是一个非常神秘的问题,你真的需要添加你所考虑的输入和预期的输出。
for combo in itertools.combinations(stuff, 15):
    if set(combo).issuperset(pair) and set(combo).issuperset(unpair):
        print(combo)