python 列表去重

第一种:

list1 = [1,2,3,4,5,6,3,4,5]

aa = {}.fromkeys(list1).keys()

print aa
[1, 2, 3, 4, 5, 6]

第二种:

aa = list(set(list1))

print aa
[1, 2, 3, 4, 5, 6]


第三种:

import itertools
ids = [1,4,3,3,4,2,3,4,5,6,1]

it = itertools.groupby(ids)

for k,g in it:
     print k,

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)

第五种:

索引再次去重,并且保持排序不变

n [25]: ids = [1,4,3,3,4,2,3,4,5,6,1]

In [26]: news_ids = list(set(ids))

In [27]: news_ids.sort(key=ids.index)

In [28]: news_ids
Out[28]: [1, 4, 3, 2, 5, 6]



你可能感兴趣的:(python 列表去重)