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']

 

上一篇:hive导数据


下一篇:100题