One - One Code All

Blog Content

python删除元素之pop,remove,del

Python   2010-02-06 13:06:30

remove 按照值删除首个符合条件的元素,并不删除特定的索引。

pop 按照索引删除字符,用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值,返回值可以付给其他的变量。

del 按照索引删除字符,返回值不可以付给其他的变量。

>>> list1 = [1,2,3,4,5,6]
>>> a=list1.pop()
>>> list1
[1, 2, 3, 4, 5]
>>> a
6
>>> list1.remove(3)
>>> list1
[1, 2, 4, 5]
>>> del(list1[1])
>>> list1
[1, 4, 5]



上一篇:python 文件头模板
下一篇:数据结构与算法学习进阶

The minute you think of giving up, think of the reason why you held on so long.