最好的方法是将你的集合转换为frozenset
s (可散列),然后使用set
只获得独特的集合,就像这样
>>> list(set(frozenset(item) for item in L))
[frozenset({2, 4}),
frozenset({3, 6}),
frozenset({1, 2}),
frozenset({5, 6}),
frozenset({1, 4}),
frozenset({3, 5})]
如果您希望将它们作为集合,那么您可以将它们转换回set
像这样
>>> [set(item) for item in set(frozenset(item) for item in L)]
[{2, 4}, {3, 6}, {1, 2}, {5, 6}, {1, 4}, {3, 5}]
如果您希望在删除重复项的同时也保持顺序,那么您可以使用collections.OrderedDict https://docs.python.org/3/library/collections.html#collections.OrderedDict, 像这样
>>> from collections import OrderedDict
>>> [set(i) for i in OrderedDict.fromkeys(frozenset(item) for item in L)]
[{1, 4}, {1, 2}, {2, 4}, {5, 6}, {3, 6}, {3, 5}]