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 dictionarylen( Dictionary name )
give an example :
person={" full name ":" Zhang San "," Age ":20," Gender ":" male "}print(len(person))
Output :
Key name in dictionary3
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 :
Bold key values in style Dictionarydict_keys([' full name ', ' Age ', ' Gender '])
<class 'dict_keys'>
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 :
The key name of the dictionary and the corresponding key valuedict_values([' Zhang San ', 20, ' male '])
<class 'dict_values'>
Dictionary name .items()
person={" full name ":" Zhang San "," Age ":20," Gender ":" male "}print(person.items())persons=person.items()print(type(persons))
Output :
Traversal of dictionariesdict_items([(' full name ', ' Zhang San '), (' Age ', 20), (' Gender ', ' male ')])
<class 'dict_items'>
Key name , Key value , Traversal of key value corresponding to key name .
Method 1give 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 :
Method 2full name Age Gender
Zhang San 20 male
(' full name ', ' Zhang San ') (' Age ', 20) (' Gender ', ' male ')
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 !