Magic two Python dictionary

# Dictionary 
#dict

#. 1 according to a sequence, to create the dictionary, and assign a uniform value

# 2 acquired according to the key value, when the key does not exist, you can specify a default value (None)

V = dict. Fromkeys ([ "K1", "123" , "999"], 123)
Print (V)

DIC = {
"K1": "V1",
"K2": "V2",
"K3": "V3"
}

. V = DIC GET ( "K1")
Print (V)

#. 3 deleted to obtain the value
V = DIC. POP ( "K1")
Print (DIC, V)

# randomly deleted
K, V = DIC. popitem ()
Print (DIC, K, V)

#. 4 set value
# of exists, is not provided, the corresponding key retrieves the current value of
# is not present, set, get the current key value corresponding to
V = DIC. setDefault ( "K4", "123")
print(dic,v)

#5更新
dic.update({"k1":"1111","k3":123})
Print (DIC)

dic.update (K1 = 123, 123 = K2, K3 = 123)
Print (DIC)

# dictionary common method
#keys () values () items ( ) get () update ()

Guess you like

Origin www.cnblogs.com/159357zzx/p/11588330.html