Operating on a dictionary

First, the definition

  A dictionary is unordered

= {DIC1 " name " : ' ZS ' , " Age " : " 30 " , " NUM " : " 1033 " }
 Print (DIC1, type (DIC1))
 # of elements in the key dictionary is a pair, the key should be is a string, the value may be any type


Second, the operating

  1) increase (change)

# Increase: 
DIC1 [ " dom " ] = (3,2)           # add a key-value pair, if it already exists is to modify the 
Print (DIC1) 
dic1.setdefault ( ' weight ' )   # have key-value pairs, without any change, not only add. 
dic1.setdefault ( ' weight ' , 100 )
 Print (DIC1) 
dic1.setdefault ( ' name ' )     # already has name, there will be no change 
Print (DIC1)

  2) deleted

# Delete 
dic1.pop ( " name " )             # delete the specified key-value pairs 
Print (DIC1) 
dic1.popitem ()               # random delete a key-value pair (plus a high probability last) 
Print (DIC1) 
dic1.clear ()                 # empty dictionary 
Print (DIC1)
 del DIC1

  3) check

= {DIC2 " Day " : " 20 is " , " Pro " : " the IT " , " NUM " : 1022 }
 for I in DIC2:            # default print key 
    Print (I)
 Print ( " === " )
 for K, V in dic2.items (): # print key-value pair, but two separate printing elements can be controlled format 
    Print (K, V)
 Print ( " === " )
 for Iin dic2.keys ():     # print key, values is the same operation, dic2.values () 
    Print (i)
 Print ( " === " )
 for i in dic2.items ():    # print key-value pairs to tuple form 
    Print (I, type (I))

 

Guess you like

Origin www.cnblogs.com/lowislucifer/p/10950443.html