Python中列表的del,remove和pop函数之间的区别

1)Python del函数 (1) Python del function)

del is nothing but "delete". del is a keyword which basically goes on the position given by the user in del(position) and deletes that element and also changes the positions of all the other elements as it is now not the part of the list.

del只是“删除”而已。 del是一个关键字,它基本上位于用户在del(position)中给定的位置上,并删除该元素,并更改所有其他元素的位置,因为它现在不在列表中。

One import thing in delete is that it takes the argument in it is id i.e. not the whole data which is to be deleted only the location of the data.

delete中的一个重要事项是它接受参数中的id,即不是要删除的整个数据,而是仅数据的位置。

2)Python移除功能 (2) Python remove function)

remove is nothing only the searching of the first occurrence of the element and removes that element.

remove只是搜索元素的第一次出现并删除该元素。

Note: "remove" is slower than the "del" as it has to search the element which makes it slow.

注意: “删除”“ del”要慢,因为它必须搜索使其变慢的元素。

3)Python pop函数 (3) Python pop function)

The pop() only takes the single argument i.e the index and removes the element from there without affecting any others position. If we pass the index which was not in the range of the given list then it through the error i.e. "IndexError: pop index out of range".

pop()仅采用单个参数,即索引,并从那里删除元素,而不会影响其他位置。 如果我们传递的索引不在给定列表的范围内,则它会通过错误即“ IndexError:pop index out of range”出现错误。

It is not necessary to pass the argument in the pop function if not passed it takes it -1 by itself and remove the element from the last and delete that location from the list.

如果未传递参数,则无需在pop函数中传递参数,否则参数本身会使其为-1并从最后一个元素中删除该元素,然后从列表中删除该位置。

列表中del,remove和pop函数的Python示例 (Python example for del, remove and pop functions in the list)

l=[1,2,3,4,6,5,6,7,8,6] #list 
# del deletes the 4th position element i.e 6  
del(l[4])  
#new list after deletion
print('After deletion:',l)   
#removes the value 6 from list
l.remove(6) 
# new list after removing
print('After removing:',l)    
#pop of the element at location 1 
l.pop(1)    
# new list after pop
print('After pop:',l)     
#pop of the element from the last of the list
l.pop(-3)    
# new list after pop
print('After pop:',l)     
#pop of the elements from the 
l.pop() 
# new list after pop
print('After pop:',l)

Output

输出量

After deletion: [1, 2, 3, 4, 5, 6, 7, 8, 6]
After removing: [1, 2, 3, 4, 5, 7, 8, 6]
After pop: [1, 3, 4, 5, 7, 8, 6]
After pop: [1, 3, 4, 5, 8, 6]
After pop: [1, 3, 4, 5, 8]

Explanation of the code:

代码说明:

    In the above code,
del(l[4])
deletes the 4th position element  i.e. 6 of the list,
and also change the position/location of all other further elements.
And, 
l.remove(6)
Removes the element 6 from the list. 
And, while using pop in list
l.pop(1)
Pops off  the first element of the list .
And, 
l.pop(-3)
Pops off the 3rd element from the last 
that means negative value means from last
And,
l.pop( )
If not given any argument by default take that -1.

翻译自: https://www.includehelp.com/python/difference-between-del-remove-and-pop-functions-of-a-list-in-python.aspx

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

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

相关文章

lcd图片转二进制工具_辽宁2.8寸LCD屏价格,测距仪LCD显示屏_思迈微

首页 > 新闻中心发布时间:2020-11-15 08:27:09 导读:思迈微为您提供辽宁寸LCD屏价格,测距仪LCD显示屏的相关知识与详情: 以上便是对LED点阵式显示屏的软硬件系统、组成等的技术知识的粗略的介绍。以这款名为《液晶显示器亮点坏点修复工具》…

python 布尔值 布尔运算 运算符

python基础语法4布尔值布尔运算运算符算术运算符赋值运算符比较运算符逻辑运算符布尔值 布尔值:Ture(真) Flase(假) 假:Flase、0、’’(空字符串)、[](空列表&#xff0…

python代码安全性问题_这个python代码对注入安全吗?

我在Python中有一个服务器/客户机套接字对。服务器接收特定的命令,然后准备响应并将其发送到客户端。在在这个问题中,我关心的只是代码中的可能的注入:如果可以要求服务器对第二个参数做一些奇怪的事情——如果对命令内容的控制不足以避免不希…

Python | 使用+ =运算符在字符串末尾附加文本

Given a string, and we have to append more string (text) at the end of the string using operator in Python. 给定一个字符串,我们必须在Python中使用 运算符在字符串的末尾附加更多字符串(文本)。 There are two methods to add string (text) at the end …

ps如何修改图片大小尺寸_PS新手入门教程:学习如何修改画布的大小

PS新手入门教程:学习如何修改画布的大小。在photoshop中,可以把画布理解为一张白纸,而我们要处理的图像可以理解为这张白纸表面上的画。我们修改画布的大小时,图像并不会随着画布的大小而整体变大或缩小,这是修改画布大…

jfinal poi

2019独角兽企业重金招聘Python工程师标准>>> 最近项目采用jfinal的项目要对一些excel进行操作,经过考虑采用jfinalpoi,在一些学习,使用后总结并分享一些代码片段。 导入excel protected Workbook workbook null;protected File filenull;publ…

python 函数 类 模块

python基础语法5函数作用域函数参数函数对象无名函数回调函数函数嵌套类类的创建类的调用初始化方法类的继承类的定制模块使用模块函数 封装好,进行某种功能 del 函数名(参数):函数体return 返回值(可以没有return语句)作用域 …

activemq nodejs stomp 重连机制_5分钟优劣分析 Kafka、RabbitMQ、RocketMQ、ActiveMQ消息队列...

一、资料文档Kafka:中,有kafka作者自己写的书,网上资料也有一些。 rabbitmq:有一些不错的书,网上资料多。 zeromq:少。没有专门写zeromq的书,网上的资料多是一些代码的实现和简单介绍。 rocketm…

excel保存快捷键_干货 | 快速提高工作效率的电脑快捷键!

点击标题下「蓝色微信名」可快速关注随着科学技术的发展,电脑已经成为每个人生活和工作的必备工具。然而,很多人在使用电脑的过程中非常依赖鼠标,导致双手需要频繁离开键盘,造成工作间歇中断,导致用电脑处理工作的效率…

16位的数字高字节和低字节_掩盖8位数字的较低和较高半字节| 8085微处理器

16位的数字高字节和低字节Problem statement: 问题陈述: To mask lower and higher nibble of 8-bit number using 8085 Microprocessor. 使用8085微处理器掩盖8位数字的较低和较高半字节。 Algorithm: 算法: Load the content of accumulator A with…

钉钉 ISV 应用开发的一些心得

1. 背景 前段时间从前到后完整地做完了一个简单的钉钉上的 ISV 应用 —— 猿活动。 最开始想做这么一个小工具,是想到,平时部门中经常会组织一些分享活动,但是这些分享活动却没有一个比较直观的“站点”来记录一次又一次的,很多人…

mcq 队列_MCQ | 8086微处理器中的字符串操作指令

mcq 队列Question 1: 问题1: A string is a collection of characters. Each Character is an of byte length which is stored at successive locations. In the 8086 microprocessor, which characters are considered in the string? 字符串是字符的集合。 每…

python 编码 解码 读写文件

python基础语法6编码解码encode编码与decode解码读写文件编码解码 计算机是以二进制(0或1)存储的,以字节为单位,1byte8bit,1KB1024B;1MB1024KB;1GB1024MB 编码表:ASCII码&#xff0…

电脑如何设置不休眠_电脑休眠了却没法唤醒?设置一下就好!

关注全新【HP惠课厅】,惠普消费新品全知晓逐步复工,办公室环境又渐渐熟悉了起来午休外出吃饭、忙里偷闲散步、下班不想关电脑……随手就把电脑休眠了开机也快,网页和工作内容也不会被关掉休眠功能是挺好用的可有时候,无论怎么点开…

node+bower+gulp+webpack初见

2019独角兽企业重金招聘Python工程师标准>>> node node模块管理是通过NPM(即 Node Package Manage,是 NodeJS 模块管理工具)来处理各模块之间的依赖。NPM按树状结构来管理的,支持某模块的不同版本。 [前提是本机已安装…

Java LinkedList公共布尔boolean offerFirst(Object o)方法(带示例)

LinkedList公共布尔布尔offerFirst(Object o)方法 (LinkedList public boolean offerFirst(Object o) method) This method is available in package java.util.LinkedList.offerFirst(Object o). 软件包java.util.LinkedList.offerFirst(Object o)中提供了此方法。 This metho…

hbuilder php mysql_xampp本地服务器+HBuilder配置php环境

HBuilder配置PHP环境:下载,运行HBuilder编辑器打开右侧小窗口,点击设置图标—>设置web服务器—>外置web服务器输入你想要浏览器运行的URL,点击两个确定,再重新点击设置web服务器,选择PHP类文件(选择之…

百度地图手机和电脑不一致_你可能不知道的电脑手机冷知识

各位好久不见呀,自科部科科又回来了!平时我们经常使用电脑,你可能以为你对电脑和手机了如指掌,然而下面的冷知识你知道多少个?01一台电脑可以有多个桌面如果你熟练的使用多个桌面这个技巧那么就可以躲着家长领导偷懒了…

嵌套字典|python_Python | 如果不是,则使用嵌套,根据销售额计算折扣

嵌套字典|pythonInput same amount and calculate discount based on the amount and given discount rate in Python. 输入相同的金额,并根据Python中的金额和给定的折扣率计算折扣。 The discount rates are: 折扣率是: Amount Discount0-5000…

RHEL 7 中 systemctl 的用法(替代service 和 chkconfig)

2019独角兽企业重金招聘Python工程师标准>>> 1、systemctl是RHEL 7 的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。可以使用它永久性或只在当前会话中启用/禁用服务。 systemctl可以列出正在运行的服务状态,如图&am…