python四种方法实现去除列表中的重复元素
转载:https://blog.csdn.net/together_cz/article/details/76201975
- 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<len(temp_list):
- 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 func1(one_list)
- print func2(one_list)
- print func3(one_list)
- print func4(one_list)
python四种方法实现去除列表中的重复元素的更多相关文章
- 兰亭集势笔试题:用最优方法从LinkedList列表中删除重复元素
用运行速度最优的方法从LinkedList列表里删除重复的元素,例如A->B->BB->B->C,返回A->B->BB->C. 考试的时候没完全想明白,考完又 ...
- 用最优方法从LinkedList列表中删除重复元素
用运行速度最优的方法从LinkedList列表里删除重复的元素,例如A->B->BB->B->C,返回A->B->BB->C. 考试的时候没完全想明白,考完又 ...
- python极简代码之检测列表是否有重复元素
极简python代码收集,实战小项目,不断撸码,以防遗忘.持续更新: 1,检测列表是否有重复元素: 1 # !usr/bin/env python3 2 # *-* coding=utf-8 *-* ...
- 去除List中的重复元素
/** * 去重list中的重复元素 * @param list * @return */ public static <T> List<T> removeRepeat(Lis ...
- python 四种方法修改类变量,实例对象调用类方法改变类属性的值,类对象调用类方法改变类属性的值,调用实例方法改变类属性的值,直接修改类属性的值
三种方法修改类变量,实例对象调用类方法改变类属性的值,类对象调用类方法改变类属性的值,调用实例方法改变类属性的值,类名就是类对象,city就是类变量, #coding=utf-8 class empl ...
- 【Java学习笔记】<集合框架>定义功能去除ArrayList中的重复元素
import java.util.ArrayList; import java.util.Iterator; import cn.itcast.p1.bean.Person; public class ...
- leetcode 82 删除排序列表中的重复元素II
与83类似,不过需要注意去除连续的重复片段的情况,如2 2 3 3这种情况,以及[1,1]这种情况下最终的cur为NULL,因此不能再令cur=cur->next; /** * Definiti ...
- JS四种方法去除字符串最后的逗号
<script> window.onload=function() { var obj = {name: "xxx", age: 30, sex: "fema ...
- python删除列表中得重复得数据
解决思想:将列表转换为 集合,利用集合删除重复数据得特性删除重复数据,然后将集合转换为列表 #删除列表中得重复元素 def delect_1 (lt): s = set(lt) lt = list(s ...
随机推荐
- java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常(转)
转:http://blog.csdn.net/gaohongijj/article/details/8010869/ 不能实例化activity有如下三种情况: 1.没有在Manifest.xml 清 ...
- pytest_用例运行级别_模块级
''' pytest 参数说明 https://www.jianshu.com/p/7a7432340f02 -x test_fixt_model.py 遇到错误时,停止运行 用-v运行(-v显示运行 ...
- three dots in git
What are the differences between double-dot “..” and triple-dot “…” in Git commit ranges? Using Comm ...
- mongodb在linux 上要注意的一些东西
没有配成开机启动服务,在bin目录下还要使用./mongod去启动,暂时先这样,另外要说的是, child process failed, exited with error number 1说明配置 ...
- 斯坦福【概率与统计】课程笔记(三):EDA | 直方图
单个定量变量的直方图表示 大家知道,定量变量是连续型变量,即不会像分类变量那样有明显的分类,那么如何将其画成直方图呢?一般来说,会将其按照某个维度来将其分组(group),举个例子. 我们有15个学生 ...
- 详解JavaScript数组过滤相同元素的5种方法
详解JavaScript数组过滤相同元素的5种方法:https://www.jb51.net/article/114490.htm
- Oracle学习笔记<4>
多表查询 1.什么是多表查询? 一次select语句需要查询的内容来自于不止一张表. 同时从多张表中查询数据. 单表查询: select id,last_name,salary from s_emp ...
- Windows server 2016远程桌面登录和修改3389端口
- docker--container之间的link,bridge create
container的name和ID一样,也是唯一的,当不知道container的IP时,可以用name替代,但需要先配置link 下面创建两个container 时,未配置link所以ping nam ...
- 44.Linked List Cycle II(环的入口节点)
Level: Medium 题目描述: Given a linked list, return the node where the cycle begins. If there is no cy ...