去除列表中的重复元素

 1 用集合
 2 ```python
 3 list(set(l))
 4 ```
 5 用字典
 6 ```python
 7 l1 = ['b','c','d','b','c','a','a']
 8 l2 = {}.fromkeys(l1).keys()
 9 print l2
10 ```
11 用列表并保持顺序
12 ```python
13 l1 = ['b','c','d','b','c','a','a']
14 l2 = list(set(l1))
15 l2.sort(key=l1.index)
16 print l2
17 ```   以下用sorted
l1 = ['b','c','d','b','c','a','a']
l2=sorted(set(l1),key=l1.index)
print(l2)

18 列表推导式
19 ```python
20 l1 = ['b','c','d','b','c','a','a']
21 l2 = []
22 [l2.append(i) for i in l1 if not i in l2]

 

posted @ 2017-08-27 21:03  人到中年万事休  阅读(121)  评论(0编辑  收藏  举报