列表去重几种方法 python

时间:2022-06-24 05:04:43

1.方法一:

>>> a=[1,1,2,2,3,3]

>>> b=[]

>>> for i in a:

...     if i not in b:

...         b.append(i)

...

>>> b

[1, 2, 3]

>>>

2.方法二:

>>> a=[1,1,2,2,3,3]

>>> for i in a:

...     for j in range(a.count(i)-1):

...         a.remove(i)

...

>>> a

[1, 2, 3]

>>>

3.方法三:

a=[1,1,2,2,3,3]

for i in a:
    while a.count(i)>1:
        a.remove(i)
print a

4.方法四:

>>> a=[1,1,2,2,3,3]

>>> list(set(a))

[1, 2, 3]

>>>

5.方法五:

>>> list({}.fromkeys([1,1,1,2,2,3,3]))
[1, 2, 3]