Example:

例:

    Input:
list1:  [10, 20, 10, 20, 30, 40, 30, 50]
Output:
List after removing duplicate elements
list2:  [10, 20, 30, 40, 50]

Logic:

逻辑:

To implement the program is too easy, we have to append elements one by one to another list by checking whether element is available in the new list or not.

要实现该程序太容易了,我们必须通过检查元素在新列表中是否可用,将元素逐个追加到另一个列表。

Let suppose, 20 is available three times in the list list1 and when we append 20 (first occurrence) to the list list2, it will be appended, but when we append 20 (second occurrence) to the list list2, condition will be false and item will not be appended. And finally, we will get list without duplicate elements.

假设,20在列表list1中有3次可用,并且当我们将20(第一次出现)附加到列表list2时 ,它将被附加,但是当我们将20(第二次出现)附加到列表list2时 ,条件将为false并且项目将不会被追加。 最后,我们将获得没有重复元素的列表。

Program:

程序:

# declare list
list1 = [10, 20, 10, 20, 30, 40, 30, 50]
# creating another list with unique elements
# declare another list
list2 = []
# appending elements
for n in list1:
if n not in list2:
list2.append(n)
# printing the lists
print "Original list"
print "list1: ", list1
print "List after removing duplicate elements"
print "list2: ", list2

Output

输出量

    Original listlist1:  [10, 20, 10, 20, 30, 40, 30, 50]List after removing duplicate elementslist2:  [10, 20, 30, 40, 50]

Program (Defining User defines function):

程序(定义用户定义功能):

# Function to remove duplicates
def removeDuplicates (list1):
# declare another list
list2 = []
# appending elements
for n in list1:
if n not in list2:
list2.append (n)
return list2
# Main code
# declare a list
list1 = [10, 20, 10, 20, 30, 40, 30, 50]
# print the list
print "Original list: ", list1
print "List after duplicate remove: ", removeDuplicates (list1)

Output

输出量

    Original list:  [10, 20, 10, 20, 30, 40, 30, 50]List after duplicate remove:  [10, 20, 30, 40, 50]

翻译自: https://www.includehelp.com/python/remove-duplicate-elements-from-the-list.aspx

Python | 程序从列表中删除重复的元素相关推荐

  1. scala 去除重复元素_Scala程序从列表中删除重复项

    scala 去除重复元素 List in Scala is a collection that stores data in the form of a liked-list. The list is ...

  2. Python快速找到列表中所有重复的元素

    Python快速找到列表中所有重复的元素:https://blog.csdn.net/sinat_29957455/article/details/103886088 index方法 为了能够找到元素 ...

  3. Python | 程序从列表中删除范围内的所有元素

    Given a list and we have to remove elements in a range from the list in Python. 给定一个列表,我们必须从Python中的 ...

  4. python从后面删除重复项_如何从Python列表中删除重复项

    如何从Python列表中删除重复项 了解如何从Python中的List中删除重复项技巧. 实例 从列表中删除任何重复项: mylist = ["a", "b", ...

  5. python去掉字典重复项_从字典列表中删除重复项python

    我正在尝试从下面的列表中删除重复项distinct_cur = [{'rtc': 0, 'vf': 0, 'mtc': 0, 'doc': 'good job', 'foc': 195, 'st': ...

  6. 如何在保留订单的同时从列表中删除重复项?

    是否有内置的程序在保留顺序的同时从Python列表中删除重复项? 我知道我可以使用集合来删除重复项,但这会破坏原始顺序. 我也知道我可以这样滚动自己: def uniq(input):output = ...

  7. 从Dart列表中删除重复项的2种方法

    本文向您展示了从 Flutter 中的列表中删除重复项的 2 种方法.第一个适用于原始数据类型列表.第二个稍微复杂一些,但适用于map****列表或对象列表. 转换为 Set 然后反转为 List 这 ...

  8. C语言从未排序的链接列表中删除重复项的算法(附完整源码)

    C语言从未排序的链接列表中删除重复项的算法 C语言从未排序的链接列表中删除重复项的算法完整源码(定义,实现,main函数测试) C语言从未排序的链接列表中删除重复项的算法完整源码(定义,实现,main ...

  9. python列表删除算法_关于算法:如何从python中的列表中删除重复的条目

    最近在一次采访中,我被要求编写一个python代码来从列表中删除所有重复的条目. 例如: Input List = {1,2,4,5,2,3,1} Expected Output List = {4, ...

最新文章

  1. Python进阶之路 3.4.2 条件语句(if、else和elif)
  2. python清空list_python如何删除list
  3. 如何编写企业解决方案书(转)
  4. 上海银行数据中心迎来智能机器“巡检员”
  5. docker安装运行迅雷thunder
  6. 架构与设计 之一 C 嵌入式设计模式(Design Patterns for Embedded Systems in C)的学习记录
  7. linux dhcp解释,教会你Suse Linux DHCP服务器配置详解
  8. zipentry压缩乱码linux,java生成压缩文件 ZipEntry出现乱码的有关问题
  9. Jquery的load加载本地文件出现跨域错误的解决方案
  10. DHCP+NAT+IP隧道
  11. 使用VSCode开发Electron的初步入门
  12. 新手刷杭电-A-2019~2025
  13. 计算机视觉行业,这_体育行业中计算机视觉的用例
  14. 深入理解C语言小括号用法
  15. Doctype作用?标准模式与兼容模式各有什么区别?
  16. everedit 格式化json_[原创]Everedit的试用限制绕过
  17. 论文笔记(一):Temporal Network Embedding with High-Order Nonlinear Information
  18. 12个黑科技网站,每个都是十分良心了 zt – 步行街主干道 – 虎扑社区
  19. 【python-opencv】灰度图和彩色图的互相转换
  20. Qt成功运行Zynq UltraScale+MPSoC平台上

热门文章

  1. 引入ui组件_Vuejs, Semantic CSS前端框架fish-ui
  2. Linux环境变量详解
  3. 科普 | 单精度、双精度、多精度和混合精度计算的区别是什么?
  4. apple quicktime怎么在ppt中用_PPT情感专题大赏No. 007:一份这就是街舞第三季主题PPT(上集)...
  5. axure实现复选框全选_jq简单的全选、反选和全不选效果
  6. pyecharts 间距_高月双色球20108期:红球首尾间距参考29区段
  7. java 多线程的同步问题_java多线程解决同步问题的几种方式,原理和代码
  8. oracle连续状态相同,数据库共有三种状态:quiesce、resrict、suspend
  9. 运行shell脚本报错:“syntax error near unexpected token fi 的解决方法
  10. VsFTP出现500 OOPS: cannot change directory的解决办法