The built-in set is easier to remember.
L1 = ['B', 'C', 'D', 'B', 'C', 'A', 'a']
L2 = List (SET (L1 ))
Print L2
There is also a speed difference that is said to be faster and never tested.
L1 = ['B', 'C', 'D', 'B', 'C', 'A', 'a']
L2 = {}. fromkeys (L1). Keys ()
Print L2
Both of them have a disadvantage. Sorting changes after removing duplicate elements:
['A', 'C', 'B', 'D']
If you want to keep their original order:
Use the sort method of the List class
L1 = ['B', 'C', 'D', 'B', 'C', 'A', 'a']
L2 = List (SET (L1 ))
L2.sort (Key = l1.index)
Print L2
You can also write it like this.
L1 = ['B', 'C', 'D', 'B', 'C', 'A', 'a']
L2 = sorted (SET (L1), Key = l1.index)
Print L2
You can also use Traversal
L1 = ['B', 'C', 'D', 'B', 'C', 'A', 'a']
L2 = []
For I in L1:
If not I in L2:
L2.append (I)
Print L2
The aboveCodeYou can also write it like this.
L1 = ['B', 'C', 'D', 'B', 'C', 'A', 'a']
L2 = []
[L2.append (I) For I in L1 if not I in L2]
Print L2
In this way, the sorting will not change:
['B', 'C', 'D', 'a']
From: http://blog.csdn.net/rainharder/article/details/5728443