How do we go about finding all sets of lists consisting of mutually disjoint subsets from a given list. Assuming that the list L
consists of a list of tuples, I tried the following code, but to no avail:
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)
But, I got the error saying that 'int' object is not iterable. I also tried the following, but to no avail:
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)
But, I got the error that there are too many values to unpack. How do we mitigate these problems. Any hints? Thanks beforehand.
CodePudding user response:
Consider this Solution using 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)