Python 删除集合列表的重复项

Python 删除集合列表的重复项,python,list,duplicates,set,unique,Python,List,Duplicates,Set,Unique,我有一个集合列表: 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]

我有一个集合列表:

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'


如何获取具有不同集合的集合列表?

最好的方法是将集合转换为
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(item) for item in set(frozenset(item) for item in L)]
[{2, 4}, {3, 6}, {1, 2}, {5, 6}, {1, 4}, {3, 5}]
>>> 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}]
如果希望将它们作为集合,则可以将它们转换回
set
s,如下所示

>>> 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(item) for item in set(frozenset(item) for item in L)]
[{2, 4}, {3, 6}, {1, 2}, {5, 6}, {1, 4}, {3, 5}]
>>> 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}]

如果您希望在删除重复项的同时维护订单,那么可以使用,如下所示

>>> 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(item) for item in set(frozenset(item) for item in L)]
[{2, 4}, {3, 6}, {1, 2}, {5, 6}, {1, 4}, {3, 5}]
>>> 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']]

一种方法是将
set
的列表转换为
list
list
,然后删除重复项,然后将
list
元素转换回
set
@ZdaR。我将用粗体表示:您的提示是错误的。按照您的说明中所说的“将
集合的列表
转换为
列表
列表
,然后移除重复项”,我们得到了
列表(map(list,[{3,11},{11,3}])
输出
[[3,11],[11,3]]
。使用
[]
列表()
创建一个list@ReblochonMasque:这是因为
[]
是文本语法(允许在编译时实例化空列表),而
list()
是函数调用(必须在运行时查找函数名,然后调用以返回空列表)。后者稍微贵一点。我对
list()
的偏好仅仅是为了美观。直到今天我都不知道它稍微贵一点。如果这是唯一的问题,我不认为这有什么大不了的。@ReblochonMasque我调查了一点,但正如你所说,这是一个微不足道的区别。@thefourtheye我认为这有点牵强,除非我遗漏了什么。您将不得不犯分配给
list
的错误,而不是将
list()
分配给某个对象。@PM2Ring它存在于2.7中,因此该语句不是严格正确的。@DanD:Oops!我最初只是看了一下,上面写着“新版本3.1”。但我刚刚查看了Python2文档&上面写着“2.7版中的新版本”;我应该去那里检查一下,看它是否被后置了。很抱歉。我将删除我的评论。这个答案是错误的。两个相等的集合可以映射到两个不同的元组。我亲眼目睹了这种情况。例如:
ss=[{3,11},{11,3}];列表(映射(元组,ss))
输出
[(3,11)、(11,3)]