1、item()方法
把字典中每對(duì)key和value組成一個(gè)元組,并把這些元組放在列表中返回。
在字典中進(jìn)行遍歷的時(shí)候可以采用item()方法將字典中的鍵值對(duì)同時(shí)遍歷出來(lái)。
2、語(yǔ)法
dict.items()
3、返回值
返回可遍歷的(鍵,值)元組數(shù)組。
4、使用實(shí)例
字典遍歷同時(shí)獲得鍵和值
>>>knights={'gallahad':'thepure','robin':'thebrave'}
>>>fork,vinknights.items():
...print(k,v)
...
gallahadthepure
robinthebrave
以上內(nèi)容為大家介紹了python中字典遍歷時(shí)如何同時(shí)獲得鍵和值?,希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。