欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中字典及遍歷常用函數(shù)的使用詳解

 更新時(shí)間:2022年06月22日 10:07:05   作者:從未止步..  
這篇文章主要為大家介紹了Python中字典有關(guān)的常見函數(shù)的使用方法,以及字典遍歷的方法。文中通過示例代碼為我們進(jìn)行了詳細(xì)介紹,對(duì)學(xué)習(xí)Python字典有一定幫助,需要的可以參考一下

字典中元素的個(gè)數(shù)計(jì)算

len(字典名)

舉例:

person={"姓名":"張三","年齡":20,"性別":"男"}
print(len(person))

輸出:

3

字典中的鍵名

字典名.keys()

舉例:

person={"姓名":"張三","年齡":20,"性別":"男"}
print(person.keys())
persons=person.keys()
print(type(persons))

輸出:

dict_keys(['姓名', '年齡', '性別'])
<class 'dict_keys'>

加粗樣式字典中的鍵值

字典名.values()

舉例:

person={"姓名":"張三","年齡":20,"性別":"男"}
print(person.values())
persons=person.values()
print(type(persons))

輸出:

dict_values(['張三', 20, '男'])
<class 'dict_values'>

字典的鍵名以及對(duì)應(yīng)的鍵值

字典名.items()

person={"姓名":"張三","年齡":20,"性別":"男"}
print(person.items())
persons=person.items()
print(type(persons))

輸出:

dict_items([('姓名', '張三'), ('年齡', 20), ('性別', '男')])
<class 'dict_items'>

字典的遍歷

鍵名,鍵值,鍵名對(duì)應(yīng)鍵值的遍歷。

方法一

舉例:

person={"姓名":"張三","年齡":20,"性別":"男"}
persons_1=person.keys()
persons_2=person.values()
persons_3=person.items()
for a in persons_1://鍵名的遍歷
    print(a,end=' ')
print("\n")
for b in persons_2://鍵值的遍歷
    print(b,end=' ')
print("\n")
for c in persons_3://鍵名與對(duì)應(yīng)的鍵值的遍歷
    print(c,end=' ')

輸出:

姓名 年齡 性別 

張三 20 男 

('姓名', '張三') ('年齡', 20) ('性別', '男') 

方法二

person={"姓名":"張三","年齡":20,"性別":"男"}
for keys in person.keys()://鍵名的遍歷
    print(keys,end=' ')
print("\n")
for values in person.values()://鍵值的遍歷
    print(values,end=' ')
print("\n")
for key,values in person.items()://鍵名與對(duì)應(yīng)的鍵值的遍歷
    print(key,values)

輸出:

姓名 年齡 性別 

張三 20 男 

姓名 張三
年齡 20
性別 男

到此這篇關(guān)于Python中字典及遍歷常用函數(shù)的使用詳解的文章就介紹到這了,更多相關(guān)Python字典遍歷內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論