本文實例講述了python實現從字典中刪除元素的方法。分享給大家供大家參考。具體分析如下:
python的字典可以通過del方法進行元素刪除,下面的代碼詳細演示了這一過程
# Create an empty dictionaryd = {}# Add an itemd["name"] = "Fido"assert d.has_key("name")# Delete the itemdel d["name"]assert not d.has_key("name")# Add a couple of itemsd["name"] = "Fido"d["type"] = "Dog"assert len(d) == 2# Remove all itemsd.clear()assert len(d) == 0
希望本文所述對大家的Python程序設計有所幫助。
新聞熱點
疑難解答