Python: Uniqueness for list of lists

后端 未结 6 1023
慢半拍i
慢半拍i 2020-11-29 00:37

I am curious what would be an efficient way of uniquefying such data objects:

testdata =[ [\'9034968\', \'ETH\'], [\'14160113\', \'ETH\'], [\'9034968\', \'ETH         


        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 00:49

    I tried @Mark's answer and got an error. Converting the list and each elements into a tuple made it work. Not sure if this the best way though.

    list(map(list, set(map(lambda i: tuple(i), testdata))))
    

    Of course the same thing can be expressed using a list comprehension instead.

    [list(i) for i in set(tuple(i) for i in testdata)]
    

    I am using Python 2.6.2.

    Update

    @Mark has since changed his answer. His current answer uses tuples and will work. So will mine :)

    Update 2

    Thanks to @Mark. I have changed my answer to return a list of lists rather than a list of tuples.

提交回复
热议问题