Python实现去除列表中重复元素的方法
这篇文章主要介绍了python实现去除列表中重复元素的方法,结合实例形式总结分析了python列表去重的4种实现方法,涉及python针对列表的遍历、判断、排序等相关操作技巧,需要的朋友可以参考下
本文实例讲述了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_listdef func4(one_list): ''''' 使用排序的方法 ''' result_list=[] temp_list=sorted(one_list) i=0 while i<len if><p></p><p>结果如下:</p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/00968c3c2c15" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">Python免费学习笔记(深入)</a>”;</p><blockquote>脚本之家测试结果:[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]</blockquote><p>运行结果截图:</p><p><img src="https://img.php.cn/upload/article/000/153/291/3e82f277a29505fb70dbf84d9fe3b41f-0.png" alt=""></p><p>