删除集合列表的重复项

removing duplicates of a list of sets

我有一个集合列表:

L = [set([1, 4]), set([1, 4]), set([1, 2]), set([1, 2]), set([2, 4]), set([2, 4]), set([5, 6]), set([5, 6]), set([3, 6]), set([3, 6]), set([3, 5]), set([3, 5])]

(实际上在我的例子中是倒数元组列表的转换)

我想删除重复项以获得:

L = [set([1, 4]), set([1, 2]), set([2, 4]), set([5, 6]), set([3, 6]), set([3, 5])]

但如果我尝试:

>>> list(set(L))
TypeError: unhashable type: 'set'

>>> list(np.unique(L))
TypeError: cannot compare sets using cmp()

如何获取包含不同集合的集合列表?

最好的方法是将您的集合转换为 frozensets(可散列),然后使用 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,像这样

>>> 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}]

使用循环的替代方法:

result = list()
for item in L:
    if item not in result:
        result.append(item)

这是另一种选择

yourNewSet = map(set,list(set(map(tuple,yourSet))))

还有另一种选择。

import itertools
list_sets = [set(['a', 'e', 'f']), set(['c', 'b', 'f']), set(['a', 'e', 'f']), set(['a', 'd']), set(['a', 'e', 'f'])]

lists = [list(s) for s in list_sets] # convert a list of sets to a list of lists
lists.sort()
lists_remove_duplicates = [lists for lists,_ in itertools.groupby(lists)]
print(lists_remove_duplicates)

# output
[['a', 'd'], ['a', 'e', 'f'], ['c', 'b', 'f']]