分类: Python/Ruby
2012-01-14 11:43:08
Python列表去掉重复元素的一种方法:
法1:
>>> L = [1, 2, 3, 4, 1, 2, 3, 4, 5]
>>> [x for x in L if x not in locals()['_[1]']]
[1, 2, 3, 4, 5]
法2:
>>> a = [11,22,33,44,11,22]
>>> b = list(set(a))
>>> b
[33, 11, 44, 22]
法1:
>>> data = [1, 2, 3, 2, 3, 'a', 'b', 'b', 'c']
>>> filter(lambda x : data.count(x) == 1, data)
[1, 'a', 'c']
>>>
法2:
[k for k in data if data .count(k) ==1];