Another variable is the dictionary container model, and can store any type of object.
Each dictionary key key => value of the colon : segmentation, key-value pair with a comma between each split, including the entire dictionary in braces {} in the following format
d = { key1 : value1 , key2 : value2 }
1, the dictionary values ([], GET)
= {S
"ID": 315,
"name": "mineral",
"Sex": "female",
"Age": 27,
"addr": "Shanghai",
"Grade": "Capricorn",
"Phone ":" 18,317,155,664 ",
" Gold ": 100
}
Print (S [" ID "]) Note: [] means, the input key is not present in [], given direct
print (s.get (" name ") ) Note: get method, there is no input of the key, return None
2, increasing the dictionary Key ([], setDefault )
s [ "man"] = "200" [] Note: The increase in the existing key, direct the development of key value of change values
s.setdefault ( "kai", "12") setdefault Note: increase the existing key, key value change in values
3, the modified key values ([])
Input key = values s [] in [ "M"] = "200"
4, delete the dictionary key (pop, del)
s.pop ( "phone") print (s) result: { 'id': 315, 'name': 'mineral water', 'sex': 'M', 'age': 27, 'addr': 'Shanghai ',' grade ':' Capricorn ',' gold ': 100,' M ': 300,' kai ':' 12 '} #pop developed delete key, delete the value returned
del s [ "name"] print (s) result: { 'id': 315, 'sex': 'M', 'age': 27, 'addr': 'Shanghai', 'grade': 'Capricorn', 'gold': 100, 'M': 300, 'kai': '12'} #del deleted established key, no return value, delete
5, the query dictionary key, query the dictionary values
s.keys () query the dictionary key
s.values () query the dictionary values
6, the dictionary circulation, remove the key, values
= {S
"ID": 315,
"name": "mineral",
"Sex": "female",
"Age": 27,
"addr": "Shanghai",
"Grade": "Capricorn",
"Phone ":" 18,317,155,664 ",
" Gold ": 100
}
for I in S:
Print (I) withdrawn dictionary key #
I in S for:
Print (I, S [I]) # remove dictionary key, values
I in S for:
values = s.get (I)
Print (I, values) taken # dictionary key, the highest efficiency values
Key for, s.items value in ():
Print (key, value) # withdrawn dictionary key, values items in the dictionary is converted into a two-dimensional array of elements, remove the key, low efficiency value
7, it is determined whether the dictionary is present key (in,)
IF "the above mentioned id" in S:
Print ( "Hello") This method is recommended
if "id" in s.keys(): print("你好")