dict and 02 additions and deletions to change search

change

= {DIC1 ' Age ' : 16, ' name ' : ' sets you elephant ' , ' Sex ' : ' , MALE ' , ' weight ' : ' 67 ' } 
DIC2 = { ' Age ' : 45, ' Sex ' : ' , MALE ' } 
dic2.update (DIC1) # update data in the dict1 dict2 in a change is not added 
Print (DIC1)
 Print (DIC2)
View Code
= {DIC1 ' Age ' : 16, ' name ' : ' sets you elephant ' , ' Sex ' : ' , MALE ' , ' weight ' : ' 67 ' } 
DIC1 [ ' Age ' ] = ' . 19 ' 
Print (DIC1 )
View Code

 check

= {DIC1 ' Age ' : 16, ' name ' : ' sets you elephant ' , ' Sex ' : ' , MALE ' , ' weight ' : ' 67 ' }
 Print (dic1.keys ()) # key 
Print (DIC1 .items ()) # value 
Print (dic1.values ()) # tuple, the tuple contains key values
View Code
= {DIC1 ' Age ' : 16, ' name ' : ' sets you elephant ' , ' Sex ' : ' , MALE ' , ' weight ' : ' 67 ' }
 # Print (dic1.keys ()) # key 
# Print (dic1.items ()) # value 
# Print (dic1.values ()) # tuple, the tuple contains key values 

for I in dic1.values ():
     Print (I)
View Code

You can also use a for loop to query, similar to the other two.

= {DIC1 ' Age ' : 16, ' name ' : ' sets you elephant ' , ' Sex ' : ' , MALE ' , ' weight ' : ' 67 ' } 
L = DIC1 [ ' Age ' ] # direct return value, However, if the key is not in the dictionary, it will error 
Print (L)
View Code

 

Guess you like

Origin www.cnblogs.com/zly9527/p/11223071.html