對(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. Django ORM實(shí)現(xiàn)按天獲取數(shù)據(jù)去重求和例子2. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐3. XML入門(mén)的常見(jiàn)問(wèn)題(一)4. jsp EL表達(dá)式詳解5. 解決ajax的delete、put方法接收不到參數(shù)的問(wèn)題方法6. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實(shí)現(xiàn)7. idea修改背景顏色樣式的方法8. chat.asp聊天程序的編寫(xiě)方法9. IntelliJ IDEA設(shè)置自動(dòng)提示功能快捷鍵的方法10. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?
