python-list-列表去重

摘要

取自网络的一些python list去重的方法。

直观方法

1
2
3
4
5
6
ids = [1,2,3,3,4,2,3,4,5,6,1]
news_ids = []
for id in ids:
if id not in news_ids:
news_ids.append(id)
print news_ids

set

1
2
ids = [1,4,3,3,4,2,3,4,5,6,1]
ids = list(set(ids))

这样的结果是没有保持原来的顺序。

按照索引再次排序

1
2
3
ids = [1,4,3,3,4,2,3,4,5,6,1]
news_ids = list(set(ids))
news_ids.sort(ids.index)

itertools.grouby

1
2
3
4
5
ids = [1,4,3,3,4,2,3,4,5,6,1]
ids.sort()
it = itertools.groupby(ids)
for k, g in it:
print k

关于itertools.groupby的原理可以看这里

reduce

1
2
3
4
ids = [1,4,3,3,4,2,3,4,5,6,1]
func = lambda x,y:x if y in x else x + [y]
print reduce(func, [[], ] + ids)
[1, 4, 3, 2, 5, 6]

其中

1
2
3
lambda x,y:x if y in x else x + [y]
等价于
lambda x,y: y in x and x or x+[y]

思路其实就是先把ids变为[[], 1,4,3,……] ,然后在利用reduce的特性。reduce解释参看这里