新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python怎樣刪除字典中的元素
python刪除字典中元素的方法:

1、使用clear()方法刪除字典內(nèi)所有元素
clear()方法語法:dict.clear()
示例:
dict = {'Name': 'Zara', 'Age': 7};
print "Start Len : %d" % len(dict)
dict.clear()
print "End Len : %d" % len(dict)輸出結(jié)果如下:
Start Len : 2
End Len : 0
2、使用pop()方法刪除字典給定鍵key及對應(yīng)的值
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'} 本文名稱:創(chuàng)新互聯(lián)Python教程:python怎樣刪除字典中的元素
網(wǎng)頁URL:http://www.dlmjj.cn/article/cdijoho.html


咨詢
建站咨詢
