python中列表元素的去重复

python中列表元素的去重复

1、方法1

>>> test1 = ["aa","bb","aa","cc","aa","cc","dd","xx","bb"]
>>> test1
['aa', 'bb', 'aa', 'cc', 'aa', 'cc', 'dd', 'xx', 'bb']
>>> test2 = []
>>> for i in test1:
    if i not in test2:
        test2.append(i)

        
>>> test2
['aa', 'bb', 'cc', 'dd', 'xx']

 

2、方法2

>>> test1
['aa', 'bb', 'aa', 'cc', 'aa', 'cc', 'dd', 'xx', 'bb']
>>> list(set(test1))
['cc', 'bb', 'aa', 'xx', 'dd']

 

上一篇:最走心的网络科普帖


下一篇:七、面向对象_11.__dict__