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

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

python四种方法实现去除列表中的重复元素的更多相关文章

  1. 兰亭集势笔试题:用最优方法从LinkedList列表中删除重复元素

    用运行速度最优的方法从LinkedList列表里删除重复的元素,例如A->B->BB->B->C,返回A->B->BB->C. 考试的时候没完全想明白,考完又 ...

  2. 用最优方法从LinkedList列表中删除重复元素

    用运行速度最优的方法从LinkedList列表里删除重复的元素,例如A->B->BB->B->C,返回A->B->BB->C. 考试的时候没完全想明白,考完又 ...

  3. python极简代码之检测列表是否有重复元素

    极简python代码收集,实战小项目,不断撸码,以防遗忘.持续更新: 1,检测列表是否有重复元素: 1 # !usr/bin/env python3 2 # *-* coding=utf-8 *-* ...

  4. 去除List中的重复元素

    /** * 去重list中的重复元素 * @param list * @return */ public static <T> List<T> removeRepeat(Lis ...

  5. python 四种方法修改类变量,实例对象调用类方法改变类属性的值,类对象调用类方法改变类属性的值,调用实例方法改变类属性的值,直接修改类属性的值

    三种方法修改类变量,实例对象调用类方法改变类属性的值,类对象调用类方法改变类属性的值,调用实例方法改变类属性的值,类名就是类对象,city就是类变量, #coding=utf-8 class empl ...

  6. 【Java学习笔记】<集合框架>定义功能去除ArrayList中的重复元素

    import java.util.ArrayList; import java.util.Iterator; import cn.itcast.p1.bean.Person; public class ...

  7. leetcode 82 删除排序列表中的重复元素II

    与83类似,不过需要注意去除连续的重复片段的情况,如2 2 3 3这种情况,以及[1,1]这种情况下最终的cur为NULL,因此不能再令cur=cur->next; /** * Definiti ...

  8. JS四种方法去除字符串最后的逗号

    <script> window.onload=function() { var obj = {name: "xxx", age: 30, sex: "fema ...

  9. python删除列表中得重复得数据

    解决思想:将列表转换为 集合,利用集合删除重复数据得特性删除重复数据,然后将集合转换为列表 #删除列表中得重复元素 def delect_1 (lt): s = set(lt) lt = list(s ...

随机推荐

  1. java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常(转)

    转:http://blog.csdn.net/gaohongijj/article/details/8010869/ 不能实例化activity有如下三种情况: 1.没有在Manifest.xml 清 ...

  2. pytest_用例运行级别_模块级

    ''' pytest 参数说明 https://www.jianshu.com/p/7a7432340f02 -x test_fixt_model.py 遇到错误时,停止运行 用-v运行(-v显示运行 ...

  3. three dots in git

    What are the differences between double-dot “..” and triple-dot “…” in Git commit ranges? Using Comm ...

  4. mongodb在linux 上要注意的一些东西

    没有配成开机启动服务,在bin目录下还要使用./mongod去启动,暂时先这样,另外要说的是, child process failed, exited with error number 1说明配置 ...

  5. 斯坦福【概率与统计】课程笔记(三):EDA | 直方图

    单个定量变量的直方图表示 大家知道,定量变量是连续型变量,即不会像分类变量那样有明显的分类,那么如何将其画成直方图呢?一般来说,会将其按照某个维度来将其分组(group),举个例子. 我们有15个学生 ...

  6. 详解JavaScript数组过滤相同元素的5种方法

    详解JavaScript数组过滤相同元素的5种方法:https://www.jb51.net/article/114490.htm

  7. Oracle学习笔记<4>

    多表查询 1.什么是多表查询? 一次select语句需要查询的内容来自于不止一张表. 同时从多张表中查询数据. 单表查询: select id,last_name,salary from s_emp ...

  8. Windows server 2016远程桌面登录和修改3389端口

  9. docker--container之间的link,bridge create

    container的name和ID一样,也是唯一的,当不知道container的IP时,可以用name替代,但需要先配置link 下面创建两个container 时,未配置link所以ping nam ...

  10. 44.Linked List Cycle II(环的入口节点)

    Level:   Medium 题目描述: Given a linked list, return the node where the cycle begins. If there is no cy ...