以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. PAT (Advanced Level) 1094. The Largest Generation (25)

    简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  2. Chapter 1 First Sight——16

    I drove around the school, following the line of traffic. 我开车绕学校随着交通线. 我开车穿过校园,紧跟着大部队. I was glad to ...

  3. Viewpager以及ViewPagerIndicator的相关使用

    ViewPagerIndicator开源框架可以用来在ViewPager上方做标题,可以在ViewPager下方做跟随移动的小圆点,这个类库必须和自己的项目在电脑的同一磁盘盘符下,比如都在D盘或者E盘 ...

  4. LDA,PCA阅读资料

    1,线性判别分析(Linear Discriminant Analysis)(一) 2,机器学习中的数学(4)-线性判别分析(LDA), 主成分分析(PCA) 3,Machine Learning i ...

  5. CNS数据库网站只用mysql自带的fulltext index功能就可实现了。

    1)编辑脚本script.sql如下 ALTER TABLE `table_name` ADD FULLTEXT (`column_name`) 2)在mysql console下输入命令 SOURC ...

  6. Android消息提示框Toast

    Android消息提示框Toast Toast是Android中一种简易的消息提示框.和Dialog不一样的是,Toast是没有焦点的,toast提示框不能被用户点击,而且Toast显示的时间有限,t ...

  7. Mysql 随机查询数据

    SELECT * FROM tablename ORDER BY RAND() LIMIT 10

  8. CF 389 E 贪心(第一次遇到这么水的E)

    http://codeforces.com/contest/389/problem/E 这道题目刚开始想的特别麻烦...但是没想到竟然是贪心 我们只需要知道偶数的时候可以对称取的,然后奇数的时候没次取 ...

  9. Swift 项目中常用的第三方框架

    Swift 项目中可能用到的第三方框架 字数1004 阅读4091 评论17 喜欢93 这里记录下swift开发中可能用的框架 , 最近浏览了不少,积累在这里,以后用的时候方便查阅.顺便推荐给大家! ...

  10. PAT (Advanced Level) 1056. Mice and Rice (25)

    简单模拟. #include<iostream> #include<cstring> #include<cmath> #include<algorithm&g ...