编程语言
首页 > 编程语言> > python中将制定元素保留为唯一

python中将制定元素保留为唯一

作者:互联网

 

>>> a = ["aa","aa","bb","cc","bb","aa","dd","cc","aa","aa","aa"]
>>> a
['aa', 'aa', 'bb', 'cc', 'bb', 'aa', 'dd', 'cc', 'aa', 'aa', 'aa']
>>> b = sorted(list(set(a)))
>>> b
['aa', 'bb', 'cc', 'dd']
>>> for i in b:
    a.count(i)

    
6
2
2
1
>>> for i in range(a.count("aa")-1):
    a.remove("aa")

    
>>> for i in b:
    a.count(i)

    
1
2
2
1
>>> a
['bb', 'cc', 'bb', 'dd', 'cc', 'aa']

 

标签:aa,count,中将,bb,python,dd,元素,cc
来源: https://www.cnblogs.com/liujiaxin2018/p/14439618.html