用内置的set
x = ['d','c','d','e','b','c','a','a'] y = list(set(x)) print y
这种有个缺点,就是去重后元素的排序变了:
['a', 'c', 'b', 'e', 'd']
如果想要保持他们的排序:
方法一:用list类的sort方法
x = ['d','c','d','e','b','c','a','a'] y = list(set(x)) y.sort(key=x.index) print y
方法二:用遍历
x = ['d','c','d','e','b','c','a','a'] y = [] for i in x: if not i in y: y.append(i) print y
这样出来的结果,list的排序就没有发生变化了:
['d', 'c', 'e', 'b', 'a']