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

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

  1. python中数组的del,remove,pop区别详解

    以a=[1,2,3] 为例,似乎使用del, remove, pop一个元素2 之后 a都是为 [1,3], 如下: >>> a=[1,2,3] >>> a.rem ...

  2. python语句中ord_浅谈Python中chr、unichr、ord字符函数之间的对比

    ord是unicode ordinal的缩写,即编号 chr是character的缩写,即字符 ord和chr是互相对应转换的. 但是由于chr局限于ascii,长度只有256,于是又多了个unich ...

  3. 树莓派i2c python_树莓派2 python i2cPython中chr、unichr、ord字符函数之间的对比

    chr.unichr.ord在Python中都可以被用作字符类型转换,这里我们就来浅谈Python中chr.unichr.ord字符函数之间的对比,需要的朋友可以参考下ord是unicode ordi ...

  4. python中列表的运用_python中列表的应用

    标签: 本文主要介绍了:python中列表的主要应用和一些列表自带的一些函数 代码: #!/usr/bin/env python # author by lh # -*- coding:utf-8 - ...

  5. 四、Python第四课——Python中列表及其操作(增删改查)

    目录 一.Python中的列表 1.列表的定义和赋值 2.列表的使用 二.列表的"增删改查" 1.列表中元素的增加 A.在列表尾添加元素 B.在列表中插入元素 2.列表中" ...

  6. 【Python】Python实战从入门到精通之二 -- 教你使用Python中列表操作

    本文是Python实战–从入门到精通系列的第二篇文章: [Python]Python实战从入门到精通之一 – 教你深入理解Python中的变量和数据类型 Python实战从入门到精通之二 – 教你使用 ...

  7. Python中列表及其操作

    文章目录 前言 一.列表简介 二.访问列表元素 三.修改.添加和删除列表元素 1.修改列表元素 2.在列表中添加元素 3.从列表中删除元素 四.对列表元素排序 五.确定列表长度 六.遍历列表 七.创建 ...

  8. python列表和元组的应用,Python中列表和元组的使用方法和区别

    一.二者区别 列表: 1.可以增加列表内容 append 2.可以统计某个列表段在整个列表中出现的次数 count 3.可以插入一个字符串,并把整个字符串的每个字母拆分当作一个列表段追加到列表当中 e ...

  9. Python中列表 list 的加减乘除运算总结

    Python中列表运算总结 列表的创建及基本用法 列表的 + - × ÷ Python中列表list是一种常用(最基本)的数据类型,其四则运算和MATLAB.numpy等数据结构不太一样. 列表的创建 ...

最新文章

  1. VoIP败家子的游戏
  2. Spring踩坑记录
  3. Linux新增开放端口
  4. icon制作无白色背景_科研立项答辩ppt制作公司
  5. 物理化学 相平衡
  6. 内网访问不到内网网站问题和不用端口号访问网站问题
  7. 微信小程序的三级分销-项目表格设计
  8. 【目标跟踪】基于matlab帧差法结合卡尔曼滤波行人姿态识别【含Matlab源码 1127期】
  9. java实时推送goeasy_JAVA Web实时消息后台服务器推送技术---GoEasy
  10. 2021年中国鱼油发展现状及进出口状况分析:我国鱼油需求进一步扩大 [图]
  11. 【寻址方式】基地址与偏移地址的详细解释
  12. 使用DFA算法对敏感词进行过滤
  13. 手把手带你搭建一个简单的webpack脚手架(一)
  14. 赛码浪潮笔试题库软件实施岗位_校园招聘在线笔试,原来大家都在用赛码
  15. 表达矩阵melt之后 画图 reshape gaochao ards 1: Unknown or uninitialised column: `p`. 2: Computation failed i
  16. 【数据结构】图(最短路径Dijkstra算法)的JAVA代码实现
  17. 再见了 VMware,一款更轻量级的虚拟机!
  18. 探索不同的文件教案计算机,小学三年级信息技术教案范文
  19. 《流浪地球》登顶IMAX国产片历史最高票房
  20. java获得windows系统当前时间与时间不符合

热门文章

  1. Nplayer本地文件拷到服务器,手把手教你简易NAS构建,手机/平板/智能电视随意调取,家庭存储云共享,有了自己的网络云盘后再也不用担心容量不够了!...
  2. xpwifi热点设置android,教你在XP电脑中开启设置WiFi热点使用的步骤
  3. mysql 查询倒数第二条记录_MySQL查询倒数第二条记录实现方法
  4. 小程序的点赞功能能和浏览次数功能_扫码点餐小程序好用吗?小程序还能实现哪些功能?...
  5. atlas安装需要kafka吗_Atlas 2.1.0 实践(2)—— 安装Atlas
  6. innobackup备份恢复实操步骤--gtid复制(1)(1)
  7. X86和X86_64和AMD64的由来
  8. Javascript基础之-原型(prototype)
  9. 【spring boot】注解@ApiParam @PathVariable @RequestParam三者区别
  10. html中可以自定义属性,,,妈的竟然才知道..