Python去除列表中的重复元素

1.用集合
2.list(set(l))
用字典
l1 = ['b','c','d','b','c','a','a']
l2 = {}.fromkeys(l1).keys()
print l2
3.用字典并保持顺序l1 = ['b','c','d','b','c','a','a']
l2 = list(set(l1))
l2.sort(key=l1.index)
print l2
4.列表推导式
l1 = ['b','c','d','b','c','a','a']
l2 = []
[l2.append(i) for i in l1 if not i in l2]
posted @ 2020-04-23 09:23  燕十三丶  阅读(494)  评论(0编辑  收藏  举报
AmazingCounters.com