Dictionary + Dictionary cycle

A dictionary definition: dict = { 'name': 'wyj', 'sex': 'M', 'high': '175'}

 

1. The list of values:

 

print {dict [ 'name']} # take if error value does not exist

print {dict.get ( 'name')} # takes a value does not exist if no error is displayed None

 

2. Increase

 

dict [ 'age'] = 18 # key, if present, to modify its value
dict.setdefault ( 'age', 18) # key, if present, it is the matter.

 

3. Modify

dict['sex'] = 女  

 

4. Delete

 

dict.pop ( 'name') # delete the specified key, it returns a value for deletion
del dict [ 'name'] # key to delete the specified

 

5. Empty dictionary

 

dict.clear()

 

print (dict.keys ()) # print all of the key dictionary

print (dict.values ​​()) # print the value of all the dictionary

Print ( dict . items ( ) )  # dictionary is converted to a list

 

6. dictionaries cycle

 

dict = {'name':'wyj','sex':'男','high':'175'}

 

for k in dict: # directly recycled dictionary, the dictionary is taken each time the key
  value = dict.get (K) # cycle time while taking the key and the value

  print (k, value) # print key and value at the same time

 

Guess you like

Origin www.cnblogs.com/wangyujian/p/11525844.html