set of list of lists in python

后端 未结 3 1952
-上瘾入骨i
-上瘾入骨i 2020-12-09 02:56

I am having a list of lists :

mat = [[1,2,3],[4,5,6],[1,2,3],[7,8,9],[4,5,6]]

and I want to convert into a set

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-09 03:17

    @thefourtheye's answer clearly depicts the problem you were facing with non-hashable data types and the way to by pass it so that you can create a set and remove duplicates. This should suffice for most of thef problems but, re-reading your question

    In above case the required answer will be [[1,2,3],[4,5,6],[7,8,9]].

    If the order is important, you need to use OrderedDict

    >>> from collections import OrderedDict
    >>> map(list, OrderedDict.fromkeys(map(tuple, mat)).keys())
    [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    

提交回复
热议问题