- l1 = ['b','c','d','b','c','a','a']
-
l2 = list(set(l1))
-
print l2
2. 还有一种据说速度更快的
l1 = ['b','c','d','b','c','a','a']
l2 = {}.fromkeys(l1).keys()
print l2
这两种都有个缺点,祛除重复元素后排序变了:
['a', 'c', 'b', 'd']
如果想保持排序
1. 用list类的sort方法
l1 = ['b','c','d','b','c','a','a']
l2 = list(set(l1))
l2.sort(key=l1.index)
print l2
2.
l1 = ['b','c','d','b','c','a','a']
l2 = sorted(set(l1),key=l1.index)
print l2
或者可以这样写
l1 = ['b','c','d','b','c','a','a']
l2 = []
[l2.append(i) for i in l1 if not i in l2]
print l2
爱生活爱技术更爱python