python去重复功能_Python实现去除列表中重复元素的方法小结【4种方法】

本文实例讲述了Python实现去除列表中重复元素的方法。分享给大家供大家参考,具体如下:

这里一共使用了四种方法来去除列表中的重复元素,下面是具体实现:

#!usr/bin/env python

#encoding:utf-8

'''

__Author__:沂水寒城

功能:去除列表中的重复元素

'''

def func1(one_list):

'''''

使用集合,个人最常用

'''

return list(set(one_list))

def func2(one_list):

'''''

使用字典的方式

'''

return {}.fromkeys(one_list).keys()

def func3(one_list):

'''''

使用列表推导的方式

'''

temp_list=[]

for one in one_list:

if one not in temp_list:

temp_list.append(one)

return temp_list

def func4(one_list):

'''''

使用排序的方法

'''

result_list=[]

temp_list=sorted(one_list)

i=0

while i

if temp_list[i] not in result_list:

result_list.append(temp_list[i])

else:

i+=1

return result_list

if __name__ == '__main__':

one_list=[56,7,4,23,56,9,0,56,12,3,56,34,45,5,6,56]

print "我们测试结果:"

print func1(one_list)

print func2(one_list)

print func3(one_list)

print func4(one_list)

结果如下:

我们测试结果:

[0, 34, 3, 4, 5, 6, 7, 9, 12, 45, 23, 56]

[0, 34, 3, 4, 5, 6, 7, 9, 12, 45, 23, 56]

[56, 7, 4, 23, 9, 0, 12, 3, 34, 45, 5, 6]

[0, 3, 4, 5, 6, 7, 9, 12, 23, 34, 45, 56]

运行结果截图:

2018427103602436.png?2018327103635

PS:本站还有两款比较简单实用的在线文本去重复工具,推荐给大家使用:

在线去除重复项工具:http://tools.jb51.net/code/quchong

在线文本去重复工具:http://tools.jb51.net/aideddesign/txt_quchong

更多关于Python相关内容可查看本站专题:《Python字典操作技巧汇总》、《Python字符串操作技巧汇总》、《Python常用遍历技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

本文标题: Python实现去除列表中重复元素的方法小结【4种方法】

本文地址: http://www.cppcns.com/jiaoben/python/226427.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值