python中删除字典中的某个元素_python怎样删除字典中的元素
生活随笔
收集整理的這篇文章主要介紹了
python中删除字典中的某个元素_python怎样删除字典中的元素
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
python刪除字典中元素的方法:
1、使用clear()方法刪除字典內所有元素
clear()方法語法:dict.clear()
示例:dict = {'Name': 'Zara', 'Age': 7};
print "Start Len : %d" % len(dict)
dict.clear()
print "End Len : %d" % len(dict)
輸出結果如下:Start Len : 2
End Len : 0
2、使用pop()方法刪除字典給定鍵key及對應的值
pop()方法語法:pop(key[,default])>>> x = {'a':1,'b':2}
>>> x.pop('a')
1
>>> x
{'b': 2}
3、使用popitem() 方法返回并刪除字典中的最后一對鍵和值。
popitem()方法語法:popitem()>>> x
{'url': 'www.iplaypython.com', 'title': 'python web site'}
>>> x.popitem()
('url', 'www.iplaypython.com')
>>> x
{'title': 'python web site'}
更多Python知識請關注Python視頻教程欄目。
總結
以上是生活随笔為你收集整理的python中删除字典中的某个元素_python怎样删除字典中的元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中为什么训练数据_pytho
- 下一篇: python json有什么用_为什么要