Nov-06-2021, 11:06 PM
>>> l = [[2,3,4,5],[2,3],[7,8],[2],[4,5,6]] >>> S = sorted((set(x) for x in l), key=len, reverse=True) >>> S [{2, 3, 4, 5}, {4, 5, 6}, {2, 3}, {8, 7}, {2}] >>> roots = [] >>> for s in S: ... if not any(s.issubset(r) for r in roots): ... roots.append(s) ... >>> roots [{2, 3, 4, 5}, {4, 5, 6}, {8, 7}]As supuflounder said, this is a O(n^2) algorithm, which means here that if the initial list has length N, we way call
issubset()
about N*(N-1)/2 times.