在日常数据的处理中,难免会遇到数据的处理,然后很多时候我们需要将重复的数据去掉,当然,我们可以写一段代码来做这个处理例如:
1 2 3 4 5 6 7 8 9 10 11 |
#!/usr/bin/env python #coding=utf-8 origh_list = [6,3,1,2,4,5,3] new_list = [] for item in origh_list: if not item in new_list: new_list.append(item) print(sorted(new_list)) |
其实,我们有更简单的方法,python已经内置了这种解决方式,那就是set
1 2 3 4 5 6 7 8 |
#!/usr/bin/env python #coding=utf-8 origh_list = [6,3,1,2,4,5,3] new_list = set(origh_list) print(new_list) |
set是一个工厂函数,它会返回给你一个没有重复项的集合,非常方便的帮我们去除重复函数
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021