当前第2页 返回上一页
1 2 3 4 | >>> str=[0,1,2,3,4,5,6]
>>> del str[2:4] #删除从第2个元素开始,到第4个为止的元素(但是不包括尾部元素)
>>> str
[0, 1, 4, 5, 6]
|
del 也可以删除整个数据对象(列表、集合等)
1 2 3 4 5 6 7 8 | >>> str=[0,1,2,3,4,5,6]
>>> del str
>>> str #删除后,找不到对象
Traceback (most recent call last):
File "<pyshell#27>" , line 1, in <module>
str
NameError: name 'str' is not defined
|
注意:del是删除引用(变量)而不是删除对象(数据),对象由自动垃圾回收机制(GC)删除。
补充: 删除元素的变相方法
1 2 3 4 5 6 7 8 9 10 11 | s1=(1,2,3,4,5,6)
s2=(2,3,5)
s3=[]
for i in s1:
if i not in s2:
s3.append(i)
print 's1-1:' ,s1
s1=s3
print 's2:' ,s2
print 's3:' ,s3
print 's1-2:' ,s1
|
以上就是如何删除 list 中指定 index 的元素的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
Python函数之chr(i)
Python实现从序列中移除重复项且保持元素间顺序不变
Python函数之compile()函数
Python多线程的优点是什么?六大优点助你了解多线程
什么是Python items函数?怎么使用它?
Python 解决中文写入excel时抛异常的问题
用Python处理图片之打开\显示\保存图像的方法
Python列表的基本操作有哪些
Python中zip是什么函数
Python文档在哪里
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 如何删除 list 中指定 index 的元素