python中去除list列表中重复数据方法

python中去除list列表中重复数据方法

2023年7月26日发(作者:)

python中去除list列表中重复数据⽅法⽅法1:将list作为set的构造函数构造⼀个set,然后再将set转换成listlist2=[10, 1, 2, 20, 10, 3, 2, 1, 15, 20, 44, 56, 3, 2, 1]list3=list(set(list2))print(list3) #set⽅法去重重复#输出结果:[1, 2, 3, 10, 44, 15, 20, 56]⽅法2:按照索引顺序排列sort() 函数⽤于对原列表进⾏排序,如果指定参数,则使⽤⽐较函数指定的⽐较函数。list2=[10, 1, 2, 20, 10, 3, 2, 1, 15, 20, 44, 56, 3, 2, 1]list3=list(set(list2))(key=) #按照索引顺序排列print(list3)#输出结果:[10, 1, 2, 20, 3, 15, 44, 56]⽅法3:循环遍历去除重复list4=[10, 1, 2, 20, 10, 3, 2, 1, 15, 20, 44, 56, 3, 2, 1]list5=[]for i in list4: if i not in list5: (i)print(list5)#输出结果:[10, 1, 2, 20, 3, 15, 44, 56]

发布者:admin,转转请注明出处:http://www.yc00.com/news/1690305973a329646.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信