Why do I need a dictionary type?
>>> list1 = ["Name", "Age", "gender"]
>>> list2 = ["Fentiao", 5, "male"]
>>> Zip (list1, List2)
Combine two lists with a zip built-in function, help (Zip)
[(' Name ', ' Fentiao '), (' Age ', 5), (' Gender ', ' male ')]
>>> list2[0]out[12]:
In direct programming, there is no understanding that the first index represents a name
' Fentiao '
>>> List2[name]
Traceback (most recent):
File "<stdin>", line 1, in <module>
Typeerror:list indices must is integers, not str
Therefore, the dictionary is the only type of mapping in Python, key-value (hash table), the Dictionary object is mutable, but the key must be an immutable object.
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/9C/18/wKioL1lsOsCQnTYYAAAisADakk0078.png "title=" 234520170717121408.png "style=" Float:none; "alt=" Wkiol1lsoscqntyyaaaisadakk0078.png "/>
-
Dictionary definition
Simple dictionary creation
>>> dic = {"name": "Fentiao", "Age": 5, "gender": "Male"}
> >> Dic[0] /Cannot extract value values by index
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
keyerror:0
>>> dic["name"] //find value values by key
' Fentiao '
Built-in method: Fromkeys
The key in the dictionary has the same value, default is None
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9C/19/wKiom1lsOOuC8AkhAAF9gxWzzs8514.png "title=" 234520170717113928.png "style=" Float:none; "alt=" Wkiom1lsoouc8akhaaf9gxwzzs8514.png "/>
650) this.width= 650, "src=" Https://s4.51cto.com/wyfs02/M01/9C/19/wKiom1lsOsLh2sCVAACstfCK240993.png "title=" 234520170717121510. PNG "style=" Float:none; "alt=" Wkiom1lsoslh2scvaacstfck240993.png "/>
Access to Dictionary values
Access directly via key
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/9C/19/wKiom1lsPgGimuDdAAE9_UcNWgM528.png "style=" float : none; "title=" 234520170717111524.png "alt=" Wkiom1lspggimuddaae9_ucnwgm528.png "/>
Loop traversal access 650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9C/19/wKiom1lsOsSRPM5wAACh_Jjy2H4942.png "title= "234520170717121628.png" style= "Float:none;" alt= "Wkiom1lsossrpm5waach_jjy2h4942.png"/>
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9C/18/wKioL1lsOsWifZJ6AAEb0gDMX90318.png "title=" 234520170717121728.png "style=" Float:none; "alt=" Wkiol1lsoswifzj6aaeb0gdmx90318.png "/>
The addition of the dictionary Key-value
Dic[key] = value through this operation, we will find that the dictionary is an unordered data type
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M02/9C/19/wKioL1lsPgLygfdXAAGPXdxr70o987.png "style=" float : none; "title=" 234520170717111650.png "alt=" Wkiol1lspglygfdxaagpxdxr70o987.png "/>
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/9C/19/wKiom1lsPgLikN17AAGcLzWfWzA370.png "title=" 234520170717111537.png "style=" Float:none; "alt=" Wkiom1lspglikn17aagclzwfwza370.png "/>
Updating the dictionary
Dic.update (DIC1)
650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M01/9C/19/wKioL1lsPgPQ2RvFAAEyD6pHObM232.png "title=" 234520170717111743.png "style=" Float:none; "alt=" Wkiol1lspgpq2rvfaaeyd6phobm232.png "/>
Deletion of dictionaries
Dic.pop (key) deletes the element of the dictionary based on the key value;
>>> DIC
{' Gender ': ' Male ', ' age ': 8, ' name ': ' Fentiao ', ' kind ': ' Cat '}
>>> dic.pop ("kind")//pop-up Dictionary with key value "kind" and return element of that key
' Cat '
>> DIC
{' Gender ': ' Male ', ' age ': 8, ' name ': ' Fentiao '}
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/9C/19/wKiom1lsOsDjw65JAADIdE6n7yA364.png "style=" float : none; "title=" 234520170717121419.png "alt=" Wkiom1lsosdjw65jaadide6n7ya364.png "/>
Dic.popitem () random Delete dictionary element, return (Key,value)
>>>dic.popitem ()
(' Gender ', ' male ')
>>> DIC
{' Age ': 5, ' name ': ' Fentiao '}
Dic.clear () Delete all elements in the dictionary
>>> dic.clear ()//delete all elements of the dictionary out[22]:
>>> DIC
{}
del dic Delete the dictionary itself
>>> del dic//delete entire dictionary
>>> DIC
Traceback (most recent):
File "<stdin>", line 1, in <module>
Nameerror:name ' dic ' is not defined
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9C/19/wKiom1lsOsHgC558AADySnL0ivQ785.png "style=" float : none; "title=" 234520170717121440.png "alt=" Wkiom1lsoshgc558aadysnl0ivq785.png "/>
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/9C/19/wKiom1lsOsOjAV-FAACXhXEUxNs926.png "style=" float : none; "title=" 234520170717121542.png "alt=" Wkiom1lsosojav-faacxhxeuxns926.png "/>
Common methods of dictionaries
Dict.get () If key exists in the dictionary, returns the corresponding value value
>>>dic.get (' age ')
5
>>>dic.get (' Gender ')
' Male '
Dic.keys () returns all key values of the dictionary
>>>dic.keys ()
[' Gender ', ' age ', ' name ']
Dic.values () Returns all value values of the dictionary
>>>dic.values ()
[' Male ', 5, ' Fentiao ']
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9C/19/wKiom1lsPWqyiJruAACWggUu0ww749.png "title=" 234520170717121455.png "alt=" Wkiom1lspwqyijruaacwgguu0ww749.png "/>
Dict.has_key () Whether a key value exists in the dictionary
>>>dic.has_key (' name ')
True
>>>dic.has_key (' age ')
True
Dic.items ()
>>>dic.items ()
[(' Gender ', ' Male '), (' Age ', 5), (' Name ', ' Fentiao ')]
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/9C/18/wKioL1lsOsPx-8hNAADhjC6XNSY809.png "style=" float : none; "title=" 234520170717121601.png "alt=" Wkiol1lsospx-8hnaadhjc6xnsy809.png "/>
Example: 650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/9C/19/wKiom1lsOsXCTbO_AAC6kwqe5dY318.png "title=" 234520170717121743.png "style=" Float:none; "alt=" Wkiom1lsosxctbo_aac6kwqe5dy318.png "/>
650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/9C/18/wKioL1lsOsXg74Y2AABZdGHXyWQ900.png "title=" 234520170717121757.png "style=" Float:none; "alt=" Wkiol1lsosxg74y2aabzdghxywq900.png "/>
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/9C/18/wKioL1lsOsHAUaOsAACWggUu0ww442.png "title=" 234520170717121455.png "style=" Float:none; "alt=" Wkiol1lsoshauaosaacwgguu0ww442.png "/>
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/9C/1F/wKioL1lsccvDY-obAAFR5ejNJBQ291.png "title=" 234520170717150922.png "style=" Float:none; "alt=" Wkiol1lsccvdy-obaafr5ejnjbq291.png "/>
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9C/1F/wKiom1lsccyCtSOqAADSx-Bi7cs928.png "title=" 234520170717150943.png "style=" Float:none; "alt=" Wkiom1lsccyctsoqaadsx-bi7cs928.png "/>