Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/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_Combinations - Fatal编程技术网

Python 组合的有效组合

Python 组合的有效组合,python,combinations,Python,Combinations,我需要一组长度不断增加的组合的所有组合。 第一组是这样创建的: combinations = list(itertools.combinations(5, 2)) [(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] 然后,匹配特定条件的所有组合存储在匹配项中: [(0, 2), (0, 3), (0, 4), (3, 4)] 其中,我想找出所有长度为3的相交组合: [(0, 3, 4

我需要一组长度不断增加的组合的所有组合。 第一组是这样创建的:

combinations = list(itertools.combinations(5, 2))
[(0, 1), (0, 2), (0, 3), (0, 4), (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
然后,匹配特定条件的所有组合存储在匹配项中:

[(0, 2), (0, 3), (0, 4), (3, 4)]
其中,我想找出所有长度为3的相交组合:

[(0, 3, 4)]

我只需要找到相互相交的组合。[(0,3)、(0,4)、(3,4)]是相交的,因此[(0,3,4)]是一个组合。[(0,2,4)]不是,因为(2,4)不相交

目前我正在使用:

 combinations = []
 for y in matches:
      for x in matches:
          if y != x:
             el = qsort(unique(y[:] + x[:]))
             if el not in combinations and len(el) == it:
                  combinations.append(el)

但是使用超过200个数字的组合需要花费太长时间。有没有更快的方法呢?

正如切普纳所说,我在这张图中寻找所有的周期或集团。Networkx的enumerate_all_cliques()是一种查找它们的简单方法。

您正在比较什么与什么?也就是说,您正在查找顶点为0、1、2、3的图中长度为3的所有循环,4,其边对应于所选的组合。您所说的
查找长度为3的所有相交组合是什么意思?可能需要的时间太长,因为您的解决方案是O(n²)。在不知道问题的情况下,很难提出优化版本。我只需要找到相互交叉的组合。[(0,3)、(0,4)、(3,4)]是相交的,因此[(0,3,4)]是一个组合。[(0,2,4)]不是,因为(2,4)不相交