-
A dictionary is another mutable container model and can store any type of object.
-
Each key value of the dictionaryKey=>valuePair with a colon:Split, with commas between each key-value pair,Split, the entire dictionary is included in curly braces{}, the format is as follows:
D = {Key1 : value1, Key2 : value2
-
The key must be unique, but the value does not have to be.
-
The value can take any data type, but the key must be immutable, such as a string, a number, or a tuple.
-
DIC = {' name ': ' Jin ', ' age ': + ' sex ': ' Male '}print (DIC) #{' name ': ' Jin ', ' sex ': ' Male ', ' age ': 18}
# # # #增 # #
-
#增 # #第一种: There is then overwrite, none add # dic = {' name ': ' Jin ', ' Age ': ', ' sex ': ' Male '}# dic[' hobby '] = ' Girl ' # print (DIC) #输出 {' Age ': 18, ' hobby ': ' girl ', ' sex ': ' male ', ' name ': ' Jin '}# dic[' name '] = ' Wusir ' # print (DIC) #{' Age ': 18, ' sex ': ' male ', ' name ': ' Wusir '}## #第二种:serdefult None added, there is no change dic = {' name ': ' Jin ', ' Age ':, ' sex ': ' Male '}# dic.setdefault (' hobby ') # print (dic) #{' sex ': ' male ', ' hobby ': none, ' age ': 18, ' name ': ' Jin '}# ' hobby ' is the key, not the value, so the dictionary will appear ' hobby ': none# dic.setdefault (' Hobby ', ' Girl ') # print (DIC) #{' name ': ' Jin ', ' hobby ': ' girl ', ' Age ': 18, ' sex ': ' male '} #键值对 ' hobby ': Girl added to the dictionary, the original dictionary does not have Hobby# dic.setdefault (' name ', ' Ritian ') # print (DIC) #{' age ': 18, ' name ': ' Jin ', ' sex ': ' male '} #原字典中有k, name ' so unchanged
# # #删 # # #
-
#pop has a return value # dic = {' name ': ' Jin ', ' age ': 18, ' Sex ': ' Male '}# print (dic.pop (' Age ')) # #返回所删除键值对的age值18 # print (DIC) # dic.pop (' Hobby ') # #报错 # Print (Dic.pop (' hobby ', None)) # #设定返回的键值, when hobby does not exist, return none# #clear empty # dic = {' name ': ' Jin ', ' Age ':, ' sex ': ' Male '}# dic.clear () # print (DIC) #{} empty the dictionary with all key-value pairs # #del # dic = {' name ': ' Jin ', ' Age ':, ' sex ': ' Male '}# del dic# print (DIC) #NameError: name ' dic ' is Not defined, by Del dic, has deleted the dictionary dic# dic = {' name ': ' Jin ', ' Age ': ' Sex ': ' Male '}# del dic[' name ']# print (DIC) #{' age ': 18, ' sex ': ' male '}, delete the key value of ' name ' in dic # dic = {' name ': ' Jin ', ' Age ':, ' sex ': ' Male '}# print (Dic.popitem ()) # print (DIC) #每次随机删除dic里面的键值对
# # # #改 #####
-
# dic = {"name": "Jin", "Age": "Sex": "Male"}# dic[' name '] = ' Taibai ' # print (DIC) #{' name ': ' taibai ', ' sex ': ' male ', ' age ': 18 Change the value of the key name # two dictionary # dic = {"name": "Jin", "Age": "Sex": "Male"}# dic2 = {" Name ":" Alex "," Weight ": 75}# dic2.update (DIC) #将dic键值对, overlay and add to Dic2# print (DIC) #{' age ': 18, ' Name ': ' Jin ', ' sex ': ' male '}# print (dic2) #{' age ': 18, ' sex ': ' male ', ' Name ': ' Jin ', ' weight ': 75}
# # # # # # # # # # # # # # #查