python 列表list去重
一.{}.fromkeys(list).keys()
list2 = {}.fromkeys(list1).keys()
list2 = {}.fromkeys(list1).keys()
(set(list1))
三.itertools.grouby
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
四,笨方法
四,笨方法ids =3,2,3333,333, ,5,6,1]news_ids = []for id in ids: if id not in news_ids: news_ids.append(id)print news_ids這四種都有個特點,去重後元素排序變了,效率據說第一種比第二種快一點 五.索引再次排序這種可以去重並且保持元素順序#要結果是[1, 4, 3, 2, 5, 6] 不要[1, 2, 3, 4, 5, 6]ids = [1,4,3,3,4,2,3,4,5,6,1]news_ids = list(set(ids))news_ids.sort(key=ids.index)print news_ids #[1, 4, 3, 2, 5, 6] 。 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]