python中给列表元素去重复
1、
>>> a = ["aa","cc","aa","bb","bb","aa","dd","ee"]
>>> a
['aa', 'cc', 'aa', 'bb', 'bb', 'aa', 'dd', 'ee']
>>> "aa" in a
True
>>> "xx" in a
False
>>> b = []
>>> b
[]
>>> for i in a:
if i not in b: ## 利用成员操作符进行去重复
b.append(i)
>>> b
['aa', 'cc', 'bb', 'dd', 'ee']
2、
>>> a = ["aa","bb","aa","cc","cc","dd","cc","aa","cc","dd","bb"]
>>> a
['aa', 'bb', 'aa', 'cc', 'cc', 'dd', 'cc', 'aa', 'cc', 'dd', 'bb']
>>> sorted(a)
['aa', 'aa', 'aa', 'bb', 'bb', 'cc', 'cc', 'cc', 'cc', 'dd', 'dd']
>>> set(a) ## set去重复
{'aa', 'dd', 'cc', 'bb'}
>>> list(set(a))
['aa', 'dd', 'cc', 'bb']
>>> sorted(list(set(a)))
['aa', 'bb', 'cc', 'dd']