python四种方法实现去除列表中的重复元素

转载:https://blog.csdn.net/together_cz/article/details/76201975

  1. def func1(one_list):  
  2.     ''''' 
  3.     使用集合,个人最常用 
  4.     '''  
  5.     return list(set(one_list))  
  6.   
  7.   
  8. def func2(one_list):  
  9.     ''''' 
  10.     使用字典的方式 
  11.     '''  
  12.     return {}.fromkeys(one_list).keys()  
  13.   
  14.   
  15. def func3(one_list):  
  16.     ''''' 
  17.     使用列表推导的方式 
  18.     '''  
  19.     temp_list=[]  
  20.     for one in one_list:  
  21.         if one not in temp_list:  
  22.             temp_list.append(one)  
  23.     return temp_list  
  24.   
  25.   
  26. def func4(one_list):  
  27.     ''''' 
  28.     使用排序的方法 
  29.     '''  
  30.     result_list=[]  
  31.     temp_list=sorted(one_list)  
  32.     i=0  
  33.     while i<len(temp_list):  
  34.         if temp_list[i] not in result_list:  
  35.             result_list.append(temp_list[i])  
  36.         else:  
  37.             i+=1  
  38.     return result_list  
  39.   
  40.   
  41. if __name__ == '__main__':  
  42.     one_list=[56,7,4,23,56,9,0,56,12,3,56,34,45,5,6,56]  
  43.     print func1(one_list)  
  44.     print func2(one_list)  
  45.    print func3(one_list)     
  46.  print func4(one_list)  

转载于:https://www.cnblogs.com/star12111/p/8830914.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值