catalogue
2.1.1 initialization dictionary
2.1.2 Access to dictionary elements
2.2 dictionary processing (method)
1. Dictionary overview
Dictionary, dictionary.
In python, the dictionary is also a very useful tool and the only mapping set in python.
Dictionary elements consist of key value pairs. The key in the dictionary is similar to the index value of the list. We usually use it to access the value of the dictionary. Unlike the list, the dictionary keys can be defined by ourselves.
Dictionary element, item; keys; values.
2. Usage
2.1 preliminary dictionary
2.1.1 initialization dictionary
1. There are two common methods for initializing Dictionaries:
>>> my_dict = {} # perhaps >>> my_dict = dict()
When using the second method to initialize the dictionary, there are several methods:
1.1 when the parameter is a tuple, there can only be one, but multiple tuples can be used as a whole with parentheses. The dict() parameter is a tuple. The first element of the tuple is the key of the dictionary, and the second element is the value of the dictionary. Example:
>>> my_dict = dict((('a',1), ('b',2), ('c',3))) >>> my_dict {'a': 1, 'b': 2, 'c': 3}
1.2 when there are multiple parameters, the format is as follows: (after input, python will automatically sort and save according to the order of keys; moreover, the parameters cannot be expressions!!!)
>>> my_dict = dict(a = 1,b = 2)#'a' = 1, this input will report an error >>> my_dict {'a': 1, 'b': 2}
1.3 ......
In a word, the second method can be used to create a dictionary as long as the parameters of dict() are mapped.
2.1.2 Access to dictionary elements
Similar to list subscript index value. We access the values of dictionary elements through keys.
>>> my_dict = dict((('a',1), ('b',2), ('c',3))) >>> my_dict {'a': 1, 'b': 2, 'c': 3} >>> my_dict['a'] 1
2.2 dictionary processing (method)
1. Add new key value pair: in this method, if the added key exists in the original dictionary, the value of the original dictionary will be overwritten; If it does not exist, a new key value pair is created.
>>> a = dict() >>> a {} >>> a['a'] = 1 >>> a {'a': 1}
2. _dict_.fromkeys(S[,v]):
Create a new dictionary.
Must exist parameter, S, dictionary key; Optional parameter, v, the value of the dictionary. The default is None.
>>> adict = {} >>> adict.fromkeys((1,2,3)) {1: None, 2: None, 3: None} >>> adict.fromkeys(('a', 'b', 'c'),0) {'a': 0, 'b': 0, 'c': 0}
Note that this method cannot be used to batch modify the values of the dictionary, because it will create a new dictionary, so that the original dictionary will be overwritten
3. Key to traverse the dictionary:_ dict_.keys();
Traversal dictionary values:_ dict_.values();
Traverse dictionary elements:_ dict_.items().
The above three methods will generate their corresponding lists. Example:
>>> adict = {} >>> adict.fromkeys(range(10), 0) {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0} >>> for num in range(10): adict[str(num)] = num**2 >>> adict.keys() dict_keys(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']) >>> adict.values() dict_values([0, 1, 4, 9, 16, 25, 36, 49, 64, 81]) >>> adict.items() dict_items([('0', 0), ('1', 1), ('2', 4), ('3', 9), ('4', 16),\ ('5', 25), ('6', 36), ('7', 49), ('8', 64), ('9', 81)])
4. _dict_.get():
Used to access dictionary elements.
The required parameter is the name of the key. The selected parameter is default and the initial value is None.
If the access object exists, return the value corresponding to the input key; If the access object does not exist, the value of default is returned.
Use the previous example:
>>> a = '5' >>> b = '10' >>> adict.get(a) 25 >>> adict.get(b) >>> #No return value >>> adict.get(b, 'error') 'error'
5._dict_.clear():
Clear dictionary elements.
Returns an empty dictionary with the same storage address.
6._dict_.copy(): shallow copy, similar to the copy() method of the list.
7. _dict_.pop():
Delete a key from the dictionary according to the parameter.
The parameter is the key you want to delete.
If the deletion is successful, the value of deletion will be returned; Deletion fails (that is, when the parameter to be deleted does not exist), KeyError is returned.
>>> adict = {'1': 1, '2': 4, '3': 9, '4': 16, '5': 25, '6': 36, '7': 49, '8': 64, '9': 81} >>> adict.pop('6') 36 >>> adict {'1': 1, '2': 4, '3': 9, '4': 16, '5': 25, '7': 49, '8': 64, '9': 81} >>> adict.pop('10') Traceback (most recent call last): File "<pyshell#7>", line 1, in <module> adict.pop('10') KeyError: '10'
Random deletion of key value pairs in the dictionary can be used_ dict_.popitem() method:
The method has no parameters. Returns the key value pair successfully deleted in tuple form.
8. _dict_.update():
The method of updating another dictionary according to the mapping method of parameters.
>>> adict = {1: 1, 2: 4, 3: 9, 4: 16, 5: None} >>> bdict = {5: 25} >>> adict.update(bdict) >>> adict {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
All of this...
See you next part...