python中列表的去重复和取重复

 

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

        
>>> test2
['aaa', 'bbb', 'ccc', 'ddd', 'eee']
>>> test1[:]
['aaa', 'bbb', 'ccc', 'aaa', 'aaa', 'bbb', 'ddd', 'eee']
>>> test3 = test1[:]
>>> test3
['aaa', 'bbb', 'ccc', 'aaa', 'aaa', 'bbb', 'ddd', 'eee']
>>> for j in test2:
    if j in test1:
        test3.remove(j)

        
>>> test1
['aaa', 'bbb', 'ccc', 'aaa', 'aaa', 'bbb', 'ddd', 'eee']
>>> test3
['aaa', 'aaa', 'bbb']

 

posted @ 2020-12-18 21:52  小鲨鱼2018  阅读(747)  评论(0编辑  收藏  举报