python中对列表去重复

 

1、

>>> test1 = ["aaa","bbb","aaa","aaa","ccc","ccc","ddd","eee"]
>>> test2 = []
>>> for i in test1:
    if i not in test2:
        test2.append(i)

        
>>> test2
['aaa', 'bbb', 'ccc', 'ddd', 'eee']

 

2、

>>> test1 = ["aaa","bbb","aaa","aaa","ccc","ccc","ddd","eee"]
>>> set(test1)
{'bbb', 'aaa', 'ccc', 'eee', 'ddd'}
>>> sorted(set(test1))
['aaa', 'bbb', 'ccc', 'ddd', 'eee']

 

posted @ 2020-12-31 23:17  小鲨鱼2018  阅读(109)  评论(0编辑  收藏  举报