Dictionary features:
- Dict disorder
- Key is the only, natural to go heavy
To create a dictionary:
Way1: Careful list pit
1 __author__="Kuankuan"2D = Dict.fromkeys ([1, 2, 3], ["name"," Age"])3 Print("Dictionary:", D)4 5D[1][0] ="mmph Good pit"6 Print("After modification:", D)7 """8 Output:9 Dictionary: {1: [' name ', ' age '], 2: [' Name ', ' Age '], 3: [' Name ', ' Age ']}Ten modified: {1: [' mmph good pit ', ' age '], 2: [' mmph good pit ', ' age '], 3: [' mmph good pit ', ' age ']} One A """
Way2:
1info = {"Teacher1":"Cang Jing Empty",2 "Teacher2":"Jing Xiang",3 "Teacher3":"Yui Hatano dress",4 "Teacher4":"Maria Ozawa"5 }6 Print("You know:", info)7 """8 Output:9 you know: {' teacher1 ': ' Cang jing empty ', ' teacher2 ': ' Jing Xiang ', ' teacher3 ': ' Yui Hatano ', ' teacher4 ': ' Maria Ozawa '}Ten """
Dictionary unordered output
Inquire
Print(info["Teacher1"])#If it doesn't exist, it will error .Print(Info.get ("Teacher5"))#This is the recommended way to return none if key does not existPrint("Teacher1" inchInfo#returns True if presentPrint("keys for the query dictionary:", Info.keys ())#the keys of the query dictionaryPrint("values for the query dictionary:", Info.values ())#values of the query dictionaryPrint(Info.items ())"""Cang jing Empty nonetrue query Dictionary of the Keys:dict_keys ([' Teacher1 ', ' teacher2 ', ' teacher3 ', ' teacher4 ']) query the Dictionary of Values:dict_values ([' Cang jing Empty ', ' Kyung-Heung ', ' Yui Hatano ', ' Maria Ozawa ']) dict_items ([' Teacher1 ', ' Cang Jing '), (' Teacher2 ', ' Jing Xiang '), (' Teacher3 ', ' Yui Hatano '), (' Teacher4 ', ' Maria Ozawa ')])"""
Modify
1 info["teacher1 " "Sky Wing "2print( " After modification: " , info) 3 # modified: {' teacher1 ': ' The Wings of the sky ', ' teacher2 ': ' Jing Xiang ', ' teacher3 ': ' Yui Hatano ', ' teacher4 ': ' Maria Ozawa '}
Increase
1info["Teacher1"] ="The wings of the Oceans"2 Print("After modification:", info)3 #modified: {' teacher1 ': ' The Wings of the sky ', ' teacher2 ': ' Jing Xiang ', ' teacher3 ': ' Yui Hatano ', ' teacher4 ': ' Maria Ozawa '}4info["Teacher5"] ="Hero"#No, it's automatically created .5 Print("Add the following dictionary:", info)6Info.setdefault ("Teacher1","Sakura Kangalia")#presence is not modified7Info.setdefault ("Teacher6","Sakura Kangalia")8 Print(Info)9b = {"Teacher1":"Cang Jing Empty",Ten "Teacher7":"Peach Valley painted in incense" One } A Info.update (b) - Print("Modified By:", info) - """ the modified: {' teacher1 ': ' The Wings of the sky ', ' teacher2 ': ' Jing Xiang ', ' teacher3 ': ' Yui Hatano ', ' teacher4 ': ' Maria Ozawa '} - added after the dictionary: {' teacher1 ': ' The Wings of the sky ', ' teacher2 ': ' Jing Xiang ', ' teacher3 ': ' Yui Hatano ', ' teacher4 ': ' Maria Ozawa ', ' teacher5 ': ' On the ex-Hui '} - {' teacher1 ': ' The Wings of the sky ', ' teacher2 ': ' Jing Xiang ', ' teacher3 ': ' Yui Hatano ', ' teacher4 ': ' Maria Ozawa ', ' teacher5 ': ' On the ex-Hui ', ' teacher6 ': ' Sakura Mai Asia '} - modified: {' teacher1 ': ' Cang jing ', ' teacher2 ': ' Jing Xiang ', ' teacher3 ': ' Yui Hatano ', ' teacher4 ': ' Maria Ozawa ', ' teacher5 ': ' On the ex-Hui ', ' teacher6 ': ' Sakura Kangalia ', ' teacher7 ': ' Peach Valley painted in incense '} + """
Delete
1 delinfo["Teacher1"]2 Print("after the deletion:", info)3Info.pop ("Teacher2")4 Print("2 after deletion:", info)5 Info.popitem ()6 Print("after random deletion:", info)7 """8 after deletion: {' teacher2 ': ' Jing Xiang ', ' teacher3 ': ' Yui Hatano ', ' teacher4 ': ' Maria Ozawa ', ' teacher5 ': ' On the original Mövenpick ', ' teacher6 ': ' Sakura Kangalia ', ' Teacher7 ' : ' Peach Valley painted in incense '}9 removed after 2: {' Teacher3 ': ' Yui Hatano ', ' teacher4 ': ' Maria Ozawa ', ' teacher5 ': ' On the original Mövenpick ', ' teacher6 ': ' Sakura Kangalia ', ' teacher7 ': ' Peach Valley painted in incense '}Ten randomly deleted: {' Teacher3 ': ' Yui Hatano ', ' teacher4 ': ' Maria Ozawa ', ' teacher5 ': ' On the ex-Hui ', ' teacher6 ': ' Sakura Kangalia '} One A """
Traversal
for i in info: print (I,info[i]) print ( " * " *50) for key,value in info.items (): # first go to the list, in the traversal, if the dictionary is too large, it slows down print (Key,value) teacher3 Yui Hatano teacher4 Maria Ozawa teacher5 Hero teacher6 Sakura Kangalia ******************************* Teacher3 Hatano Teacher4 Maria Ozawa teacher5 Hero teacher6 Sakura Kangalia
Empty
1 info.clear () 2 Print (" emptied:", info) 3 # empty after: {}
Python Learning Path-dictionary dict common methods