Python 数据列表简单去重

1.新列表去重

def remove_same(list):
    list.sort() # 不需要重新排序的话删除此句即可
    new_list = []
    for i in list:
       if i not in new_list:
           new_list.append(i)

    return new_list


lists = [1,3,2,4,9,2,3,5,6,7,10,66,23,33,4,2]
remove_same(lists)

2.pop删除

def remove_same(list):
    list.sort() # 不需要重排序删除此句即可
    # new_list = []
    for i in list:
       if lists.count(i) >= 2:
           list.pop(list.index(i)) # pop()参数是元素的索引

    return list


lists = [1,3,2,4,9,2,3,5,6,7,10,66,23,33,4,2]
remove_same(lists)

3.set删除

注:set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。

def remove_same(list):
    new_list = set(list)
    return new_list


lists = [1,3,2,4,9,2,3,5,6,7,10,10,66,23,33,4,2,3]
remove_same(lists)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值