python中给列表元素去重复

 

1、

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

        
>>> b            ## for循环去重复
['aaa', 'bbb', 'ccc', 'ddd']
>>> list(set(a))    ## set 去重复
['bbb', 'ccc', 'ddd', 'aaa']
>>> c = a[:]
>>> c == a
True
>>> for i in c:
    while c.count(i) != 1:       ## while循环去重复
        c.remove(i)

        
>>> c
['bbb', 'aaa', 'ccc', 'ddd']

 

posted @ 2021-02-24 21:59  小鲨鱼2018  阅读(239)  评论(0编辑  收藏  举报