亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python中pop()函數(shù)的語(yǔ)法與實(shí)例

 更新時(shí)間:2020年12月01日 11:12:35   作者:awen_5  
這篇文章主要給大家介紹了關(guān)于python中pop()函數(shù)語(yǔ)法與實(shí)例的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

語(yǔ)法:

列表 list.pop(obj=list[-1])
pop()用于刪除并返回列表中的一個(gè)元素(默認(rèn)為最后一個(gè)元素)

obj:要?jiǎng)h除并返回的列表元素

字典dict.pop(key[,default])
pop()用于刪除字典中給定的key及對(duì)應(yīng)的value,返回被刪除key對(duì)應(yīng)的value,key值必須給出。給定的key值不在字典中時(shí),返回default值。

key:需要?jiǎng)h除的key值(不給出會(huì)報(bào)錯(cuò))

default:若沒(méi)有字典中key值,返回default值(給定的key值不在字典中時(shí)必須設(shè)置,否則會(huì)報(bào)錯(cuò))

實(shí)例:

列表

>>> list1 = [1,2,4,"hello","xy","你好"]
>>> a = list1.pop()#默認(rèn)彈出最后一個(gè)元素
>>> print(a,list1)
你好 [1,2,4,"hello","xy"]
>>> list2 = [1,2,4,"hello","xy","你好"]
>>> b = list2.pop(3)#彈出列表中第四個(gè)元素
>>> print(b,list2)
hello [1,2,4,"xy","你好“]

字典

>>> dict1 = {"papa":"xy","sis":"nikki","dude":"cwy"}
>>> c = dict.pop()#不給定key值報(bào)錯(cuò)
Traceback (most recent call last):
 File "<pyshell#2>", line 1, in <module>
  c = dict.pop()
TypeError: unbound method dict.pop() needs an argument
>>> dict1 = {"papa":"xy","sis":"nikki","dude":"cwy"}
>>> c = dict1.pop("papa")
>>> print(c,dict1)
xy {'sis': 'nikki', 'dude': 'cwy'}
>>> dict2 = {"papa":"xy","sis":"nikki","dude":"cwy"}
>>> d = dict2.pop("www")#給定鍵不在字典內(nèi)時(shí),未設(shè)置default值報(bào)錯(cuò)
Traceback (most recent call last):
 File "<pyshell#15>", line 1, in <module>
  d = dict2.pop("www")
KeyError: 'www'
>>> dict2 = {"papa":"xy","sis":"nikki","dude":"cwy"}
>>> d = dict2.pop("www","不在字典內(nèi)")
>>> print(d,dict2)
不在字典內(nèi) {'papa': 'xy', 'sis': 'nikki', 'dude': 'cwy'}

總結(jié)

到此這篇關(guān)于python中pop()函數(shù)語(yǔ)法與實(shí)例的文章就介紹到這了,更多相關(guān)python中pop()函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論