python 数组的del ,remove,pop区别
以a=[1,2,3] 为例,似乎使用del, remove, pop一个元素2 之后 a都是为 [1,3], 如下:
- >>> a=[1,2,3]
- >>> a.remove(2)
- >>> a
- [1, 3]
- >>> a=[1,2,3]
- >>> del a[1]
- >>> a
- [1, 3]
- >>> a= [1,2,3]
- >>> a.pop(1)
- 2
- >>> a
- [1, 3]
- >>>
那么Python对于列表的del, remove, pop操作,它们之间有何区别呢?
首先,remove 是删除首个符合条件的元素。并不是删除特定的索引。如下例: 本文来自Novell迷网站 http://novell.me
- >>> a = [0, 2, 2, 3]
- >>> a.remove(2)
- >>> a
- [0, 2, 3]
而对于 del 来说,它是根据索引(元素所在位置)来删除的,如下例:
- >>> a = [3, 2, 2, 1]
- >>> del a[1]
- [3, 2, 1]
第1个元素为a[0] --是以0开始计数的。则a[1]是指第2个元素,即里面的值2.
最后我们再看看pop
- >>> a = [4, 3, 5]
- >>> a.pop(1)
- 3
- >>> a
- [4, 5]
pop返回的是你弹出的那个数值。
所以使用时要根据你的具体需求选用合适的方法。 内容来自http://novell.me
另外它们如果出错,出错模式也是不一样的。注意看下面区别:
- >>> a = [4, 5, 6]
- >>> a.remove(7)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- ValueError: list.remove(x): x not in list
- >>> del a[7]
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- IndexError: list assignment index out of range
- >>> a.pop(7)
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- IndexError: pop index out of range
python 数组的del ,remove,pop区别的更多相关文章
- python删除列表元素remove,pop,del
python删除列表元素 觉得有用的话,欢迎一起讨论相互学习~Follow Me remove 删除单个元素,删除首个符合条件的元素,按值删除,返回值为空 List_remove = [1, 2, 2 ...
- Python中list的删除del&remove小区别
del删除时候指定下标,remove必须指定具体的值
- Python中remove,del和pop的区别
以a=[1,2,3] 为例,似乎使用del, remove, pop一个元素2 之后 a都是为 [1,3], 如下:http://Novell.Me >>> a=[1,2,3] &g ...
- [Python基础]Python中remove,del和pop的区别
以a=[1,2,3] 为例,似乎使用del, remove, pop一个元素2 之后 a都是为 [1,3], 如下:http://Novell.Me >>> a=[1,2,3] &g ...
- Python中remove,pop,del的区别
先上题:写出最终打印的结果 a = [1, 2, 3, 4] for x in a: a.remove(x) print(a) print("=" * 20) b = [1, 2, ...
- python的append insert extend pop del remove使用
对于 python 数组的操作,有插入和删除,下面介绍各个函数的功能: 插入 插入的函数有 append.insert .extend append append(i) 是在数组的末尾插入一个元素 i ...
- python数组的使用
python数组的使用 2010-07-28 17:17 1.Python的数组分三种类型:(1) list 普通的链表,初始化后可以通过特定方法动态增加元素.定义方式:arr = [元素] (2) ...
- python数组(列表、元组及字典)
python数组的使用 2010-07-28 17:17 1.Python的数组分三种类型: (1) list 普通的链表,初始化后可以通过特定方法动态增加元素. 定义方式:arr = [元素] (2 ...
- Python数组使用
python数组的使用 2010-07-28 17:17 1.Python的数组分三种类型: (1) list 普通的链表,初始化后可以通过特定方法动态增加元素. 定义方式:arr = [元素] (2 ...
随机推荐
- jsp导入外部样式
在博客园遇到一位朋友,她将我的代码下载下去,运行的时候,jsp页面的样式不存在,不会显示出来. 这里就将我之前写的SpringSpringmvcMybatis做一些修改. jdk1.8 加入了两个ja ...
- messages exchanged between the client's and server's computers will never be lost, damaged, or received out of order. [1]
w几乎所有的HTTP通信都由TCP/IP承载. HTTP The Definitive Guide Just about all of the world's HTTP communication i ...
- 利用未文档化API:RtlAdjustPrivilege 提权实现自动关机
这里主要是利用NTDLL.dll中未文档化的API: RtlAdjustPrivilege 来实现提权.自动关机的功能. RtlAdjustPrivilege定义如下: NTSTATUS RtlAdj ...
- discuz 添加板块失败解决办法
最近把服务器环境升了下级,发现discuz后台添加栏目添加不了了,数据库没变,源代码没变,就突然添加不了了.刚开始添加1个板块成功了,再添加就怎么也添不进去了.只是页面刷新了一下,啥提示没有. 经过一 ...
- CF 476 div2 C
http://www.codeforces.com/contest/476/problem/C C. Dreamoon and Sums time limit per test 1.5 secon ...
- FACE++学习一、detect接口
/detection/detect 描述 检测给定图片(Image)中的所有人脸(Face)的位置和相应的面部属性 目前面部属性包括性别(gender), 年龄(age), 种族(race), 微笑程 ...
- 在 Android 中调用二进制可执行程序(native executable )
前几天有需要在java代码中调用二进制程序,就在网上找了些资料,写点东西记录下. Android 也是基于linux 的系统,当然也可以运行二进制的可执行文件.只不过Android 限制了直接的方式只 ...
- 根据html生成Word文件,包含图片
根据html内容生成word,并自动下载下来.使用到了itext-1.4.6.jar import java.io.File; import java.io.FileInputStream; impo ...
- jQuery 查找带有某一属性的元素
$('*[name="username"]') 要在前面加个*表示所有的DOM,如果只是查找带有name属性的DOM的话则是这样的 $('*[name]')//其实, $('[ ...
- 强制修改mysql 中root的密码
/etc/init.d/mysqld stop (service mysqld stop )/usr/bin/mysqld_safe --skip-grant-tables另外开个SSH连接[ro ...