as a python list follow
list1 = [[1,2],[3,4],[1,2]]
I want to create a set so that I can create unique list items like
list2 = [[1,2],[3,4]].
Is there any function in python that I can use. Thanks
This will do:
>>> list1 = [[1,2],[3,4],[1,2]] >>> list2 = list(map(list, set(map(tuple,list1)))) >>> list2 [[1, 2], [3, 4]]
Unfortunately, there is no single built-in function that can handle this. The lists are not shaking (see this SO post) . So you cannot have setfrom listin Python.
set
list
But the tuples are hashed:
l = [[1, 2], [3, 4], [1, 2]] s = {tuple(x) for x in l} print(s) # out: {(1, 2), (3, 4)}
, , , , append, , . , uniquification .
append
, , . : [[1,2,3], [1,2], [1]] → [[1,2,3], [1,2], [1]]
[[1,2,3], [1,2], [1]]
>>> print map(list, {tuple(sublist) for sublist in list1}) [[1, 2], [3, 4]]
:
list1 = [[1,2],[3,4],[1,2]] list2 = [] for i in list1: if i not in list2: list2.append(i) print(list2) [[1, 2], [3, 4]]
, :
Python 2.x
list1 = [[1, 2], [3, 4], [1, 2]] list2 = {str(v): v for v in list1}.values()
Python 3.x
list1 = [[1, 2], [3, 4], [1, 2]] list2 = list({str(v): v for v in list1}.values())
There is no integrated single function to achieve this. You got a lot of answers. In addition to these, you can also use the function lambdato achieve this goal:
lambda
list(map(list, set(map(lambda i: tuple(i), list1))))
Source: https://habr.com/ru/post/1652208/More articles:How to setup scala sbt project for nd4j and deeplearning4j - scalaHow to update recyclerview when an action is deleted that is open from the recycliewiew adapter - androidXamarin: check if any particular application is installed on an iOS device - c #Why make lists inept? - pythonKryo: deserialize an old version of a class - scalaProblem on Angular 2 RC5 with global pipes - angularjsZero context in setUserVisibleHint - javaIOS Issues with IPv6 and Azure - iosXamarin Form, iOS Networking Support for IPv6 Only - iosXamarin iOS IPv6 crashes Apple Store - iosAll Articles