程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Detailed explanation of the use of dictionary and traversal common functions in Python

編輯:Python

Catalog

Count the number of elements in the dictionary

Key name in dictionary

Bold key values in style Dictionary

The key name of the dictionary and the corresponding key value

Traversal of dictionaries

Method 1

Method 2

Count the number of elements in the dictionary

len( Dictionary name )

give an example :

person={" full name ":" Zhang San "," Age ":20," Gender ":" male "}print(len(person))

Output :

3

Key name in dictionary

Dictionary name .keys()

give an example :

person={" full name ":" Zhang San "," Age ":20," Gender ":" male "}print(person.keys())persons=person.keys()print(type(persons))

Output :

dict_keys([' full name ', ' Age ', ' Gender '])
<class 'dict_keys'>

Bold key values in style Dictionary

Dictionary name .values()

give an example :

person={" full name ":" Zhang San "," Age ":20," Gender ":" male "}print(person.values())persons=person.values()print(type(persons))

Output :

dict_values([' Zhang San ', 20, ' male '])
<class 'dict_values'>

The key name of the dictionary and the corresponding key value

Dictionary name .items()

person={" full name ":" Zhang San "," Age ":20," Gender ":" male "}print(person.items())persons=person.items()print(type(persons))

Output :

dict_items([(' full name ', ' Zhang San '), (' Age ', 20), (' Gender ', ' male ')])
<class 'dict_items'>

Traversal of dictionaries

Key name , Key value , Traversal of key value corresponding to key name .

Method 1

give an example :

person={" full name ":" Zhang San "," Age ":20," Gender ":" male "}persons_1=person.keys()persons_2=person.values()persons_3=person.items()for a in persons_1:// Traversal of key names print(a,end=' ')print("\n")for b in persons_2:// Traversal of key value print(b,end=' ')print("\n")for c in persons_3:// Traversal of key name and corresponding key value print(c,end=' ')

Output :

full name Age Gender  

Zhang San 20 male  

(' full name ', ' Zhang San ') (' Age ', 20) (' Gender ', ' male ') 

Method 2 person={" full name ":" Zhang San "," Age ":20," Gender ":" male "}for keys in person.keys():// Traversal of key names print(keys,end=' ')print("\n")for values in person.values():// Traversal of key value print(values,end=' ')print("\n")for key,values in person.items():// Traversal of key name and corresponding key value print(key,values)

Output :

full name Age Gender  

Zhang San 20 male  

full name Zhang San
Age 20
Gender male

This is about Python This is the end of the article on the use of Chinese dictionary and traversal of common functions , More about Python Please search the previous articles of SDN or continue to browse the following related articles. I hope you will support SDN more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved