温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python对list去重的各种方法

发布时间:2020-07-17 09:14:16 来源:网络 阅读:535 作者:jedi911 栏目:编程语言

参考原文:https://www.the5fire.com/python-remove-duplicates-in-list.html

需求:去list进行去重,去重后保证顺序不变

方法1:for循环

ids = [1, 2, 3, 3, 4, 2, 3, 4, 5, 6, 1]
new_ids = []

for id in ids:
    if id not in new_ids:
        new_ids.append(id)

print("new_ids==>", new_ids)

方法2:set

ids = [1,4,3,3,4,2,3,4,5,6,1]
new_ids = list(set(ids))

print(new_ids)

测试发现去重后不能保证原来的顺序

方法3:按照索引再次排序

ids = [1, 4, 3, 3, 4, 2, 3, 4, 5, 6, 1]
new_ids = list(set(ids))
new_ids.sort(key=ids.index)

print(new_ids)

方法4:用reduce

ids = [1,4,3,3,4,2,3,4,5,6,1]
func = lambda x,y:x if y in x else x + [y]
reduce(func, [[], ] + ids)
[1, 4, 3, 2, 5, 6]

其中的 lambda x,y:x if y in x else x + [y] 等价于 lambda x,y: y in x and x or x+[y] 。
思路其实就是先把ids变为[[], 1,4,3,......] ,然后在利用reduce的特性

reduce()函数介绍

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI