在列表中查找不共享元素(没有任何重叠)的 3 元素集组

Find groups of 3-element sets that DO not share elements (no overlap whatsoever) in a list

我找到了一种算法,可以对列表进行排序并显示集合列表的每个值,并找到所有不重叠的集合。

例子

c = [[1,2,3],[4,3,2],[4,5,6],[7,8,9]]

for a in range(0, len(c)):
    for b in range(o, len(c)):
        if c[a] does not overlap c[b]:
            new_list.append(c[a] and c[b]:
              # example [1,2,3],[4,5,6]
            if c[b] does not overlap all of new_list:
              # example can't use [4,3,2] because 4 is already in one of the sets
               new_list.append([7,9,8])

预期输出

[1,2,3],[4,5,6],[7,8,9]

问题

python 中是否有任何预先存在的排序算法可以按照我的意图进行?

这是您的算法的一个版本,它使用一个集合来检查列表中的值之前是否已被看到:

c = [[1,2,3],[4,3,2],[4,5,6],[9,5,8],[7,8,9]]

new = []
s = set()

for l in c:
    if not any(v in s for v in l):
        new.append(l)
        s.update(l)

print(new)

输出:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]