對(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. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))2. JavaWeb Servlet中url-pattern的使用3. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息4. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式5. HTML5 Canvas繪制圖形從入門到精通6. 使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫(kù)遷移7. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲8. .Net Core和RabbitMQ限制循環(huán)消費(fèi)的方法9. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法10. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明
