查找具有 python 的列表的相互不相交的子集

Finding Mutually disjoint subsets of a list with python

我们如何从给定列表中找到由相互不相交的子集组成的所有列表集。假设列表 L 由元组列表组成,我尝试了以下代码,但无济于事:

L=[Large list of lists of tuples]
K=[]
for i in L:
      for j in len(L):
                if set(i).intersect(set(L[j]))==Integer(0):
                   K.append(i,L[j])
       list(K)

但是,我收到错误消息说 'int' 对象不可迭代。我也尝试了以下,但无济于事:

L=[Large list of list of tuples]
K=[]
 for i,j in L:
     if set(i).intersect(set(j))==0:
         K.append(i,j)
 list(K)

但是,我得到的错误是要解包的值太多。我们如何缓解这些问题。有什么提示吗?提前致谢。

考虑使用 itertools 的解决方案:

import itertools

ls = [[1,2],[2,3],[3,4]]
k = []

# for all 2 element combinations of set
for i,j in itertools.combinations(ls,2):
    # an empty set is True, a nonempty set is False
    if not set(i).intersection(set(j)):
        k.append((i,j))
    
print(k)