python——?jiǎng)h除任意鍵值對(duì)
提問(wèn)人:楊紫紅發(fā)布時(shí)間:2020-11-20
d9 = {1:"one", 2:"two", 3:"three"}
print d9.pop(1) #one
print d9 #{2: 'two', 3: 'three'}
print d9.pop(5, None) #None
try:
d9.pop(5) # raise KeyError
except KeyError, ke:
print "KeyError:", ke #KeyError:5
popitem() ---刪除任意鍵值對(duì),并返回該鍵值對(duì),如果字典為空,則產(chǎn)生異常KeyError
繼續(xù)查找其他問(wèn)題的答案?
相關(guān)視頻回答
點(diǎn)擊加載更多評(píng)論>>