對(duì)Python 字典元素進(jìn)行刪除的方法
1. Python字典的clear()方法(刪除字典內(nèi)所有元素)
#!/usr/bin/python# -*- coding: UTF-8 -*-dict = {’name’: ’我的博客地址’, ’alexa’: 10000, ’url’: ’http://blog.csdn.net/uuihoo/’}dict.clear(); # 清空詞典所有條目
2. Python字典的pop()方法(刪除字典給定鍵 key 所對(duì)應(yīng)的值,返回值為被刪除的值)
#!/usr/bin/python# -*- coding: UTF-8 -*-site= {’name’: ’我的博客地址’, ’alexa’: 10000, ’url’:’http://blog.csdn.net/uuihoo/’}pop_obj=site.pop(’name’) # 刪除要?jiǎng)h除的鍵值對(duì),如{’name’:’我的博客地址’}這個(gè)鍵值對(duì)print pop_obj # 輸出 :我的博客地址
3. Python字典的popitem()方法(隨機(jī)返回并刪除字典中的一對(duì)鍵和值)
#!/usr/bin/python# -*- coding: UTF-8 -*-site= {’name’: ’我的博客地址’, ’alexa’: 10000, ’url’:’http://blog.csdn.net/uuihoo/’}pop_obj=site.popitem() # 隨機(jī)返回并刪除一個(gè)鍵值對(duì)print pop_obj # 輸出結(jié)果可能是{’url’,’http://blog.csdn.net/uuihoo/’}
4. del 全局方法(能刪單一的元素也能清空字典,清空只需一項(xiàng)操作)
#!/usr/bin/python# -*- coding: UTF-8 -*-site= {’name’: ’我的博客地址’, ’alexa’: 10000, ’url’:’http://blog.csdn.net/uuihoo/’}del site[’name’] # 刪除鍵是’name’的條目 del site # 清空字典所有條目
以上就是對(duì)Python 字典元素進(jìn)行刪除的方法的詳細(xì)內(nèi)容,更多關(guān)于Python 刪除字典元素的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 詳解盒子端CSS動(dòng)畫(huà)性能提升2. 利用CSS制作3D動(dòng)畫(huà)3. CSS hack用法案例詳解4. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?5. XML入門(mén)的常見(jiàn)問(wèn)題(二)6. css代碼優(yōu)化的12個(gè)技巧7. 怎樣打開(kāi)XML文件?xml文件如何打開(kāi)?8. 詳解瀏覽器的緩存機(jī)制9. asp取整數(shù)mod 有小數(shù)的就自動(dòng)加110. CSS3中Transition屬性詳解以及示例分享
