Python list Go heavy
A. {}.fromkeys (list). Keys ()
List2 = {}.fromkeys (list1). Keys ()
Two. Set
List2 = List (set (List1))
Three. Itertools.grouby
ids = [1,4,3,3,4,2,3,4,5,6,1]
Ids.sort ()
it = itertools.groupby (IDs)
For K, G in it:
Print K
Four, stupid method
ids = [1,2,3,3,4,2,3,4,5,6,1]
News_ids = []
For ID in IDs:
If ID not in News_ids:
News_ids.append (ID)
Print News_ids
There are four kinds of features, the re-ordering of the elements after the change, efficiency is said to be the first faster than the second kind of
Five. The index is sorted again this can be de-weighed and maintain element order
#要结果是 [1, 4, 3, 2, 5, 6] do not [1, 2, 3, 4, 5, 6]
ids = [1,4,3,3,4,2,3,4,5,6,1]
News_ids = List (set (IDS))
News_ids.sort (Key=ids.index)
Print News_ids #[1, 4, 3, 2, 5, 6]
VI: Reduce
ids = [1,4,3,3,4,2,3,4,5,6,1]
Func = lambda x,y:x if y in x else x + [y]
Print reduce (func, [[],] + IDs) #[1, 4, 3, 2, 5, 6]