首页 > 编程语言 > 详细

python中从列表中删除元素

时间:2021-04-28 21:57:21      阅读:30      评论:0      收藏:0      [点我收藏+]

python中从列表中删除元素

remove:删除指定元素

pop:删除特定索引位置元素

del:删除变量

1、remove

>>> test1 = ["aa","bb","cc","dd","ee","ff","gg"]
>>> test1
[‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘, ‘ee‘, ‘ff‘, ‘gg‘]
>>> test1.remove("aa")
>>> test1
[‘bb‘, ‘cc‘, ‘dd‘, ‘ee‘, ‘ff‘, ‘gg‘]
>>> test1.remove("dd")
>>> test1
[‘bb‘, ‘cc‘, ‘ee‘, ‘ff‘, ‘gg‘]

 

2、pop

>>> test1
[‘bb‘, ‘cc‘, ‘ee‘, ‘ff‘, ‘gg‘]
>>> test1.pop()
‘gg‘
>>> test1
[‘bb‘, ‘cc‘, ‘ee‘, ‘ff‘]
>>> test1.pop(2)
‘ee‘
>>> test1
[‘bb‘, ‘cc‘, ‘ff‘]

 

3、del

>>> test1
[‘bb‘, ‘cc‘, ‘ff‘]
>>> del test1[1]
>>> test1
[‘bb‘, ‘ff‘]
>>> del test1[0]
>>> test1
[‘ff‘]
>>> del test1
>>> test1
Traceback (most recent call last):
  File "<pyshell#259>", line 1, in <module>
    test1
NameError: name ‘test1‘ is not defined

 

python中从列表中删除元素

原文:https://www.cnblogs.com/liujiaxin2018/p/14715536.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!