Python 列表内元素去重

最简单的方法:

a = [1,1,3,3,4,4,6,6,2,2]
b = set(a)
a = list(b)
print(a)

[1, 2, 3, 4, 6]

优点:简洁
缺点:列表排序打乱

保留原排序的方法:

a = [1,1,3,3,4,4,6,6,2,2]
c = []
for i in a:
    if i not in c:
        c.append(i)
print(c)

[1, 3, 4, 6, 2]
发布了44 篇原创文章 · 获赞 0 · 访问量 1697

猜你喜欢

转载自blog.csdn.net/qq_42067550/article/details/105527141