刪除 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+ 觀看

開啟你的 職業生涯

透過完成課程獲取認證

開始
廣告