as with lists and strings, dictionaries also have methods.
Clear clears all items in the dictionary.
We can assign an empty dictionary to a dictionary to empty its entries. You can also completely empty the dictionary using the Clear method.
x= {}y=xx[' age ']= ' + ' x[' gender ']= ' Male ' Print x x.clear () print x print y {' gender ': ' Male ', ' age ': ' 25 '}{}{}
If you change X.clear to x={}, the result becomes
{' Gender ': ' Male ', ' age ': ' 25 '} {} {' gender ': ' Male ', ' age ': ' 25 '}
2. Shallow copy and deep copy
The former refers to a value of modify copy's dictionary, which in the original dictionary also changes, and vice versa.
x= {}x[' age ']=[' + ', ' + ', ' []] #Listx [' Gender ']= ' Male ' #Stringy =x.copy () x[' age '].remove (' + ') y[' age '].remove (' 26 ') Print x print y {' gender ': ' Male ', ' age ': [']}{' gender ': ' Male ', ' age ': [' 28 ']}
If you use deep copy deepcopy, the original dictionary and the copy dictionary are completely independent
From copy import deepcopyx= {}x[' age ']=[', ' + ', ' + '] #Listx [' Gender ']= ' Male ' #Stringy =deepcopy (x) x[' age '].append ( ' + ') y[' age '].remove (' + ') print x print y {' gender ': ' Male ', ' age ': [' + ', ' + ', ' + ', ' + ']}{' gender ': ' Male ', ' age ': [' 2 5 ', ' 28 ']}
3. Fromkeys creates a dictionary with the given keys, the default value is None
What errors do you see here?
x= {}.fromkeys (' name ', ' gender ') print x {' A ': ' Gender ', ' e ': ' Gender ', ' m ': ' Gender ', ' N ': ' Gender '}
The correct form for
x= {}.fromkeys ([' Name ', ' gender ']) print x {' Gender ': none, ' Name ': none}
4. Get can access items that do not exist in a dictionary
x= {}print x.get (' age ') None
More flexibility in responding to items that are not in the user input dictionary
A few simple ways:
5. Has_key is used to check if a dictionary has a specified key, and it can be implemented in.
6. Pop is used to get the value of the given key and delete the item from the dictionary.
info = {' Alice ': {' phone ': ' 2342 ', ' addr ': ' Taierzhuang Rd '}, ' Bob ': { ' Phone ': ' 2242 ', ' addr ': ' Jinqiao Rd '}}info.pop (' Alice ') print info{' Bob ': {' phone ': ' 2242 ', ' Ad Dr ': ' Jinqiao Rd '}}
7.setdefault is used to add a key (you can add a key if the key does not exist, unlike get, it will change the original dictionary)
8.items & Iteritems
Items returned as a list of items in the dictionary, Iteritems returns an iterator
info = { ' Alice ': { ' phone ': ' 2342 ', ' addr ': ' Taierzhuang rd '}, ' Bob ':{ ' phone ': ' 2242 ', ' addr ': ' Jinqiao rd '} }print infoprint info.items () Print info.iteritems () {' Bob ': {' phone ': ' 2242 ', ' addr ': ' jinqiao rd '}, ' Alice ': {' phone ': ' 2342 ', ' ' addr ': ' Taierzhuang rd '}}[(' Bob ', {' phone ': ' 2242 ', ' addr ': ' jinqiao rd '}), (' Alice ', {' phone ': ' 2342 ', ' addr ': ' Taierzhuang rd '})]<dictionary-itemiterator object at 0x10a200a48>
The same, keys and Iterkeys return keys, values, and itervalues return value.
9.update update the other with one dictionary item.
d={' title ': ' Python Web site ', ' url ': ' http://www.python.org ', ' changed ': ' April 4 20:18 '}x={' title ': ' Pyth On Language Website '}print d.update (x)
Note that at this point the output is none, do not know why, put the last step above to write separately:
D.update (x) print d{' url ': ' http://www.python.org ', ' changed ': ' April 4 20:18 ', ' title ': ' Python Language Website '}
Python Learning notes-dictionaries (bottom)