如何从列表中创建集合?

时间:2017-11-02 15:43:31

标签: python python-3.x

我有很多列表,需要在python中创建列表/一组唯一列表。我尝试在setunique_lists.update({list_name}))中执行此操作,但收到错误:TypeError: unhashable type: 'list'。我可以用另一种方式创建它而不是用另一个列表检查每个列表的循环吗?一些很棒的模块?

l1 = [1, 2, 3]
l2 = [1, 4, 6]
l3 = [1, 2, 3]

output = [[1, 2, 3], [1, 4, 6]]

1 个答案:

答案 0 :(得分:1)

使用itertools我们可以做到这一点

>>> import itertools
>>> lst
[[1, 2, 3], [1, 4, 6], [1, 2, 3]]
>>> lst.sort()
>>> lst
[[1, 2, 3], [1, 2, 3], [1, 4, 6]]
>>> list(lst for lst,_ in itertools.groupby(lst))
[[1, 2, 3], [1, 4, 6]]
>>>