删除 Python 中元组元素


无法移除单个元组元素。当然,将另一个元组和不需要的元素放在一起是没有错的。

要显式删除整个元组,只需使用 del 语句。

实例

实时演示

#!/usr/bin/python
tup = ('physics', 'chemistry', 1997, 2000);
print tup;
del tup;
print "After deleting tup : ";
print tup;

输出

会产生以下结果。请注意引发了一个异常,这是因为在 del tup 之后元组不再存在 −

('physics', 'chemistry', 1997, 2000)
After deleting tup :
Traceback (most recent call last):
File "test.py", line 9, in <module>
print tup;
NameError: name 'tup' is not defined

更新于: 2020 年 1 月 28 日

8K+ 观看

开启你的 职业生涯

通过完成课程获取认证

开始
广告
© . All rights reserved.