Python | 程序从列表中删除重复的元素

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542225.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux的简介与虚拟机的管理

Linux的简介: 严格的来讲,Linux不算是一个操作系统,只是一个Linux系统中的内核,Linux的全称是GUN/Linux,这才算是一个真正意义上的Linux系统。 Linux是一个多用户多任务的操作系统,拥有良好的用户界面&…

python递归查找_Python程序使用递归查找数字的幂

python递归查找Given the base x and the power y and we have to find the x to the power y using recursion in Python. 给定基数x和幂y ,我们必须使用Python中的递归找到x到幂y 。 By using recursion – We will be multiplying a number (initially with val…

phalapi可以依赖注入么_PHP 依赖注入

通常调用一个类里面的方法需要如何操作:$class new class();$class->fun()依赖注入模式用来减少程序间的耦合依赖注入共有三种模式:setter 方法注入着重说下setter方法注入并结合ArrayAccess/*** Class Di* property People*/class Di implements Ar…

R语言:ggplot2精细化绘图——以实用商业化图表绘图为例(转)

本文旨在介绍R语言中ggplot2包的一些精细化操作,主要适用于对R画图有一定了解,需要更精细化作图的人,尤其是那些刚从excel转ggplot2的各位,有比较频繁的作图需求的人。不讨论那些样式非常酷炫的图表,以实用的商业化图表…

Linux中常用的命令

1.文件建立 touch file(文件的名字) 注意: touch不但可以建立文件也可以修改文件的时间戳 时间戳分为: atime:文件内容被访问的时间标识 mtime:文件内容被修改的时间标识 ctime:文件属性或文件内…

蓝桥杯宝藏排序题目算法(冒泡、选择、插入)

冒泡排序: def bubble_sort(li): # 函数方式for i in range(len(li)-1):exchangeFalsefor j in range(len(li)-i-1):if li[j]>li[j1]:li[j],li[j1]li[j1],li[j]exchangeTrueif not exchange:return 选择排序: 从左往右找到最小的元素,放在起始位置…

hive分区用2个字段有何限制_[特性]Hive动态分区功能使用

[特性]Hive动态分区功能使用2016-01-31 21:40说明Hive有两种分区,一种是静态分区,也就是普通的分区。另一种是动态分区。动态分区在数据导入时,会根据具体的字段值自行决定导入,并创建相应的分区。使用上更为方面。举例准备工作创…

Linux系统中输出输入的管理

1.什么是输入和输出 输入和输出是计算机系统中的主机与外部进行通信的系统。它由外围设备和输入输出控制系统两部分组成,我们在shell中键入指令,然后送入CPU中运算产生结果,再将结果送到字符设备中显示。简单点来说输入输出就是通过我们的键盘…

find 命令示例_数组find()方法以及JavaScript中的示例

find 命令示例JavaScript find()方法 (JavaScript find() method) find() method is used to get the first element from an array which passes the given test (condition). find()方法用于从通过给定测试(条件)的数组中获取第一个元素。 Syntax: 句法: array.…

统计Apache或Nginx访问日志里的独立IP访问数量的Shell

1、把IP数量直接输出显示: cat access_log_2011_06_26.log |awk ‘{print $1}’|uniq -c|wc -l 2、把IP数量输出到文本显示: cat access_log_2011_06_26.log |awk ‘{print $1}’|uniq -c|wc -l > ip.txt 总结:如果单个访问日志大小超过2G…

ggplot2箱式图两两比较_R绘图 第四篇:绘制箱图(ggplot2)

箱线图通过绘制观测数据的五数总括,即最小值、下四分位数、中位数、上四分位数以及最大值,描述了变量值的分布情况。箱线图能够显示出离群点(outlier),离群点也叫做异常值,通过箱线图能够很容易识别出数据中的异常值。箱线图提供了…

Linux系统中用户的管理

#####用户管理###### 1在Linux中,有三种用户: 1 root : 也成为超级用户,对系统有控制权限,超级用户可以不受限制的运行任何命令,root 用户可以看作是系统的管理员。 2 系统用户: 系统用户通常为系统功能所必…

c# 命名空间命名规范_C#命名空间能力问题和解答 套装3

c# 命名空间命名规范1) There are following namespaces are given below, which is correct about "using" statement in C#.NET? In C#.Net, "using" statement is used to import the namespace in our programWe can create a new namespace with the…

shell 查出文件并复制到另一个文件夹

找出所有大于100M的文件并展示出来find / -size 100M -exec ls -lh {} \;找出特定文件内大于200字节的文件并备份到另一个文件夹里去find /opt/test -type f -size 200c -exec cp {} /opt/test/cp/ \;转载于:https://blog.51cto.com/406647516/1875417

correl函数相关系数大小意义_用Correl函数返回相关系数,以确定属性关系

我们辛辛苦苦制作了表格,当然是要作出分析的,肯定不能就是这么几个数据吧。常用的分析法都是图表,虽然看起来直观,但是对于非作者来说,理解意思显然不是那么方便。下面,教大家使用函数,来算出相…

Java之类的构造器(反射)

反射: Java反射机制:指的是在Java程序运行状态中,对于任何一个类,都可以获得这个类的所有属性和方法;对于给定的一个对象,都能够调用它的任意一个属性和方法。这种动态获取类的内容以及动态调用对象的方法称为反射机制。Java的反射机制允许在对类未知的情…

java 系统自动检测_如何在Java中检测OS(操作系统)名称?

java 系统自动检测To detect the OS (operating system) name in Java, we use the getProperties() method, which is defined in System class, while calling the method, we need to pass the property name to get the OS (operating system name). 要检测Java中的OS(操作…

shell中返回值是1为真还是假_shell脚本中判断上一个命令是否执行成功

SQL Server 系列文章快速导航(SWF版)一.前言 在博客园写博客不自不觉已经有5个年头了,一开始只是为了记录工作中遇到的问题和解决办法,后来写的文章不自不觉的侧重在SQL Server方面的技术文章,在2014年1月终于鼓起勇气申请了微软S ...duilib帮助1.窗口基类:见介绍 顺便贴下出来…

Linux中对进程的管理

1.what is 进程 程序(program)放置在储存媒体中(如硬盘、光盘、软盘、磁盘等),为实体的型态存在。 进程:程序被触发后,执行者的权限与属性、程序的程序码与所需数据等都会被载入内存中&#xff…

带C#示例的String.Equality(==)运算符

C#String.Equality运算符 (C# String.Equality operator ) "" is a String.Equality operator in C#, it is used to check whether two strings objects have the same values or not. “ ”是C#中的String.Equality运算符 ,用于检…