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

  1. >>> a=[1,2,3]
  2. >>> a.remove(2)
  3. >>> a
  4. [1, 3]
  5. >>> a=[1,2,3]
  6. >>> del a[1]
  7. >>> a
  8. [1, 3]
  9. >>> a= [1,2,3]
  10. >>> a.pop(1)
  11. 2
  12. >>> a
  13. [1, 3]
  14. >>>

那么Python对于列表的del, remove, pop操作,它们之间有何区别呢?

首先,remove 是删除首个符合条件的元素。并不是删除特定的索引。如下例: 本文来自Novell迷网站 http://novell.me

  1. >>> a = [0, 2, 2, 3]
  2. >>> a.remove(2)
  3. >>> a
  4. [0, 2, 3]

而对于 del 来说,它是根据索引(元素所在位置)来删除的,如下例:

  1. >>> a = [3, 2, 2, 1]
  2. >>> del a[1]
  3. [3, 2, 1]

第1个元素为a[0] --是以0开始计数的。则a[1]是指第2个元素,即里面的值2.

最后我们再看看pop

  1. >>> a = [4, 3, 5]
  2. >>> a.pop(1)
  3. 3
  4. >>> a
  5. [4, 5]

pop返回的是你弹出的那个数值。

所以使用时要根据你的具体需求选用合适的方法。 内容来自http://novell.me

另外它们如果出错,出错模式也是不一样的。注意看下面区别:

  1. >>> a = [4, 5, 6]
  2. >>> a.remove(7)
  3. Traceback (most recent call last):
  4. File "<stdin>", line 1, in <module>
  5. ValueError: list.remove(x): x not in list
  6. >>> del a[7]
  7. Traceback (most recent call last):
  8. File "<stdin>", line 1, in <module>
  9. IndexError: list assignment index out of range
  10. >>> a.pop(7)
  11. Traceback (most recent call last):
  12. File "<stdin>", line 1, in <module>
  13. IndexError: pop index out of range

python 数组的del ,remove,pop区别的更多相关文章

  1. python删除列表元素remove,pop,del

    python删除列表元素 觉得有用的话,欢迎一起讨论相互学习~Follow Me remove 删除单个元素,删除首个符合条件的元素,按值删除,返回值为空 List_remove = [1, 2, 2 ...

  2. Python中list的删除del&remove小区别

    del删除时候指定下标,remove必须指定具体的值

  3. Python中remove,del和pop的区别

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

  4. [Python基础]Python中remove,del和pop的区别

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

  5. Python中remove,pop,del的区别

    先上题:写出最终打印的结果 a = [1, 2, 3, 4] for x in a: a.remove(x) print(a) print("=" * 20) b = [1, 2, ...

  6. python的append insert extend pop del remove使用

    对于 python 数组的操作,有插入和删除,下面介绍各个函数的功能: 插入 插入的函数有 append.insert .extend append append(i) 是在数组的末尾插入一个元素 i ...

  7. python数组的使用

    python数组的使用 2010-07-28 17:17 1.Python的数组分三种类型:(1) list 普通的链表,初始化后可以通过特定方法动态增加元素.定义方式:arr = [元素] (2) ...

  8. python数组(列表、元组及字典)

    python数组的使用 2010-07-28 17:17 1.Python的数组分三种类型: (1) list 普通的链表,初始化后可以通过特定方法动态增加元素. 定义方式:arr = [元素] (2 ...

  9. Python数组使用

    python数组的使用 2010-07-28 17:17 1.Python的数组分三种类型: (1) list 普通的链表,初始化后可以通过特定方法动态增加元素. 定义方式:arr = [元素] (2 ...

随机推荐

  1. EL表达式,保留小数点后两位

    你遇到过页面显示小数有9.987870488E9这个吗? 这是因为没有保留小数的原因 有时候用js保留小数很麻烦的时候,可以用EL表达式 <fmt:formatNumber type=" ...

  2. listview设置条目点击的时候不变色(让状态选择器不起作用)

    未设置前的效果如下图: 很明显,“酷狗音乐”那个条目被点击的时候,条目背景变为蓝色,怎么去掉这个颜色呢? java代码可以这么写: listView.setSelector(new ColorDraw ...

  3. Linux 安装 Redis 服务

    下载地址 http://download.redis.io/releases/redis-3.2.0.tar.gz 官网下载地址 http://redis.io/download 1.下载安装包 cd ...

  4. gridview如何隐藏一列数据,但又可以使用这列数据

    解决方案在RowCreated事件中书写如下代码 void GridView1_RowCreated(object sender, GridViewRowEventArgs e)    {       ...

  5. VS2005混合编译ARM汇编代码-转

    原文地址:http://blog.csdn.net/annelcf/article/details/5468093 公司HW team有人希望可以给他们写一个在WinCE上,单独读写DDR的工具,以方 ...

  6. java中单例设计模式

    在java中创建单例的方式主要有三种:饿汉式.懒汉式.登记式.以下内容均是摘抄自 http://blog.csdn.net/jason0539/article/details/23297037/ 一. ...

  7. OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波

    http://blog.csdn.net/chenyusiyuan/article/details/8710462 OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波 201 ...

  8. VS找不到MFC90d.dll错误

    VS 2005/VS 2008在生成可执行文件时使用了一种新的技术,该技术生成的可执行文件会伴随生成一个清单文件(manifest file)(.manifest后缀文件)(其本质上是XML文档,你可 ...

  9. background系列属性

    1.background-color背景颜色属性 ①颜色表示方法 英语单词:red   blue   purple    skyblue. rgb:r代表红色   g代表绿色   b代表蓝色    也 ...

  10. The 2014 ACMICPC Asia Invitational Xian

    上半年邀请赛的时候真是险而又险地2题拿了个铜,确实其实跟没拿一样......现场前复盘一下,长长记性 [A]签到题 [B]最短路+DFS [C]最短路 [D]构造+欧拉回路 [E]数论,最佳平方逼近 ...