python中实现列表中指定元素保留唯一

 

>>> test1 = ["ccc","aaa","aaa","ccc","bbb","aaa","ddd","aaa"]
>>> test1.count("aaa")
4
>>> for i in range(test1.count("aaa")-1):
    test1.remove("aaa")

    
>>> test1
['ccc', 'ccc', 'bbb', 'ddd', 'aaa']
>>> test1.count("aaa")
1

 

posted @ 2020-12-26 14:06  小鲨鱼2018  阅读(958)  评论(0编辑  收藏  举报