Python 列表元素排重uniq

 1 # -*- coding: gbk -*-
 2 
 3 def uniq(ls):
 4     lsCopy=[e for e in ls]
 5     for i in xrange(1,len(ls)):
 6         for j in xrange(i):
 7             if ls[j]!=ls[i]:
 8                 pass
 9             else:
10                 lsCopy.remove(ls[i])#如果有removeAt(index)方法更好
11     return lsCopy
12 
13 if __name__=='__main__':
14     ls=[1,2,3,4,5,6,7,8,9,7,2,3,6]
15     print uniq(ls) 
结果:[1, 4, 5, 8, 9, 7, 2, 3, 6]#remove(e)删除时从列表头开始找e

 

posted @ 2016-04-29 15:56  morein2008  阅读(1474)  评论(0编辑  收藏  举报