我找到了一种算法,该算法将对列表进行排序并显示集合列表的每个值,并找到所有不重叠的集合。
示例
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中是否有任何可以按照我的意愿执行的排序算法?
答案 0 :(得分:2)
这是您的算法的一种版本,该算法使用一组检查以前是否已查看过列表中的值:
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]]