The dictionary is Python The only built-in function that implements the mapping relationship in . The mapping relation is equivalent to Morse code , One to one correspondence , On the data acquisition of mapping type , Dictionaries are often faster than lists . The key symbols in the dictionary are braces {} . aggregate :set, Dictionaries :dict, It is usually in the form of a pair of key value pairs , key The colon value , In the code : Lv Bu is the key , Mouth cloth is worth , There is a colon in the middle .
Dictionaries :dict>>> x = {" Lyu3 bu4 ", " Guan yu "}
>>> type(x)
<class 'set'>
>>> y = { " Lyu3 bu4 " :" Mouth cloth ", " Guan yu " : " Close Xi Xi "}
>>> type(y)
<class 'dict'>
You can get the value through the index key , If you specify a key that does not exist in the dictionary, you can create a new key value pair
>>> y[" Lyu3 bu4 "]
' Mouth cloth '
>>> y[" Liu bei "] = " Liu baby"
>>> y
{' Lyu3 bu4 ': ' Mouth cloth ', ' Guan yu ': ' Close Xi Xi ', ' Liu bei ': ' Liu baby'}
Six ways to create a dictionary
The first one is : Use a combination of braces and colons directly , Tie the mapping relationship to .
>>> a = {" Lyu3 bu4 " : " Mouth cloth ", " Guan yu " : " Close Xi Xi ", " Liu bei " : " Liu baby"}
The second kind : Use dict() function , Between each parameter is a corresponding key value pair , An equal sign is used directly between the key and the value , Be careful :
>>> b = { Lyu3 bu4 = " Mouth cloth ", Guan yu = " Close Xi Xi ", Liu bei = " Liu baby"} // Wrong writing
SyntaxError: invalid syntax
>>> b = dict( Lyu3 bu4 =" Mouth cloth ", Guan yu =" Close Xi Xi ", Liu bei =" Liu baby") // Write it correctly , notes : You can't put quotation marks on keys
The third kind of : Use the list as a parameter , Each element in the list is a key value pair wrapped in tuples .
>>> c = dict([(" Lyu3 bu4 " , " Mouth cloth "), (" Guan yu " , " Close Xi Xi "), (" Liu bei " , " Liu baby")])
A fourth : Pass the first method as a parameter to dict() function
>>> d = dict({" Lyu3 bu4 " : " Mouth cloth ", " Guan yu " : " Close Xi Xi ", " Liu bei " : " Liu baby"})
The fifth : Mixing method
>>> e = dict({" Lyu3 bu4 " : " Mouth cloth ", " Guan yu " : " Close Xi Xi "}, Liu bei = " Liu baby")
6 kinds of :zip() function : Create an iterator that aggregates multiple iteratible objects , Can be passed as a parameter to dict() function
>>> f = dict(zip([" Lyu3 bu4 ", " Guan yu ", " Liu bei "], [" Mouth cloth ", " Close Xi Xi ", " Liu baby"]))
There are also additions, deletions, and changes in the dictionary
increase :fromkeys(iterable[,values]) have access to iterable Parameter to create a new dictionary , And initialize all the values to values The value specified by the parameter .
>>> d = dict.fromkeys("fish" ,250)
>>> d
{'f': 250, 'i': 250, 's': 250, 'h': 250}
>>> d['f'] = 70 // You can change the value of the key
>>> d
{'f': 70, 'i': 250, 's': 250, 'h': 250}
>>> d['c'] = 66 // If you can't find the key , You can add a new key value pair
>>> d
{'f': 70, 'i': 250, 's': 250, 'h': 250, 'c': 66}
The difference between a dictionary and a sequence : Elements in a sequence can be repeated , And the key value pairs in the dictionary , It has a key for a value , There are no duplicate keys , If it is repeated, the old value is overwritten with the new value .
Delete :pop(key[,default])
>>> d.pop('s')
250
>>> d
{'f': 70, 'i': 250, 'h': 250, 'c': 66}
If pop A nonexistent key will throw an exception
>>> d.pop('a')
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
d.pop('a')
KeyError: 'a'
popitem() stay 3.7 After the version , Its function is to delete the last key value pair added to the dictionary
>>> d.popitem()
('c', 66)
>>> d
{'f': 70, 'i': 250, 'h': 250}
del Keyword can also be used to delete a specified dictionary element , You can also add the name of the dictionary directly , It is equivalent to killing the whole dictionary , The dictionary doesn't exist .
>>> del d['i']
>>> d
{'f': 70, 'h': 250}
>>> del d
>>> d
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
d
NameError: name 'd' is not defined
clear() Method : Only clear the contents of the dictionary
>>> d = dict.fromkeys("fish" ,250)
>>> d
{'f': 250, 'i': 250, 's': 250, 'h': 250}
>>> d.clear()
>>> d
{}