This article mainly introduces the dictionary in python, which is one of the most powerful data types in Python, this article explains what is a dictionary, how to create a dictionary and assign values to the dictionary, basic operations on the dictionary, ing type operators, ing related functions, and dictionary methods.
1. what is a dictionary?
A dictionary is the only ing type in Python.
In a ing object, the hash value (key, key) has a one-to-many relationship with the object (value, value), which is generally considered a variable hash table.
The dictionary object is variable. it is a container type that can store any number of Python objects, including other container types.
Differences between the dictionary type and the sequence type:
1. the methods for accessing and accessing data are different.
2. sequence types only use numeric keys (index from the beginning of the sequence in numerical order );
3. the ing type can use other object types as keys (such as numbers, strings, and ancestor, usually strings as keys). different from the sequence type keys, the ing type keys are straight 4. connect or indirectly associate with stored data values.
5. data in the ing type is unordered. This is different from the sequence type. the sequence type is arranged in numerical order.
6. the ing type is directly "mapped" to the value with the key.
A dictionary is one of the most powerful data types in Python.
II. how to create a dictionary and assign values to the dictionary
In short, the dictionary is a set of key-value pairs enclosed in braces. (Key-value pairs are also called items)
General format:
The code is as follows:
Adict = {}
Adict = {key1: value2, key2: value2 ,...}
Or use the dict () function, for example, adict = dict () or adict = dict (['X', 1], ['y', 2]). is this correct? Adict = dict (['X', 1], ['y', 2]). Keyword parameter creation dictionary, such as: adict = dict (name = 'Allen', age = '40 ′)
Or use the fromkeys () method, for example, adict = {}. fromkeys ('X', 'y'),-1) creates the same dictionary value. If no value is given, the default value is None.
Features:
1. separate keys and values with the colon;
2. separate items with commas;
3. the keys in the dictionary must be unique, but the values may not be unique.
The code is as follows:
Adict = {'name': 'Allen ', 'name': 'Lucy', 'age': '40'} and bdict = {'name': 'Allen ', 'name2': 'Allen ', 'age': '40 ′}
Note: If the value in the dictionary is a number, it is best to use a string number, for example, 'age': '040' instead of 'age': 040
3. basic dictionary operations
1. how to access the value in the dictionary?
Return the value corresponding to the key in the form of adict [key]. if the key is not in the dictionary, a KeyError is thrown.
2. how do I check whether the key is in the dictionary?
A. The has_key () method is like: adict. haskey ('name') has-> True, none-> False
B. in and not in shapes such as: 'name' in adict has-> True, no-> False
3. how to update the dictionary?
A. add a data item (new element) or key-value pair
Add an item in the form of adict [new_key] = value
B. update a data item (element) or key-value pair
Adict [old_key] = new_value
C. delete a data item (element) or key-value pair
Del adict [key] delete the key item/del adict delete the entire dictionary
Adict. pop (key) deletes the key and returns the value corresponding to the key.
IV. ing type operators
Standard type operators (+,-, *, <,>, <=, >=, = ,! =, And, or, not)
A. The dictionary does not support concatenation and repetition operators (+ ,*)
B. Comparison of dictionaries
First, compare the length of the dictionary, that is, the number of dictionary elements.
Key comparison
Value Comparison
Example:
The code is as follows:
Adict = {}
Bdict = {'name': 'Allen ', 'age': '40 ′}
Cmp (adict, bdict) <->-1 or>-> 1 or =-> 0
V. ing functions
1. len () returns the length of the dictionary.
2. hash () returns the hash value of an object, which can be used to determine whether an object can be used as a dictionary key.
3. dict () factory functions used to create dictionaries
6. dictionary methods
1. adict. keys () returns a list of all keys in the dictionary;
2. adict. values () returns a list containing all values in the dictionary;
3. adict. items () returns a list containing all (key, value) ancestor;
4. adict. clear () deletes all items or elements in the dictionary;
5. adict. copy () returns a copy of the dictionary;
6. adict. fromkeys (seq, val = None) is created and a new dictionary is returned. the dictionary key is used as an element in seq, val is the initial value corresponding to all keys in the dictionary (the default value is None );
7. adict. get (key, default = None) returns the value corresponding to the key in the dictionary. if the key does not exist in the dictionary, the default value is returned (default is None );
8. adict. has_key (key) returns True if the key is in the dictionary; otherwise, False is returned. Use in, not in;
9. adict. iteritems (), adict. iterkeys (), adict. itervalues () is the same as their non-iterative methods. The difference is that they return an iterator instead of a list;
10. the adict. pop (key [, default]) and get methods are similar. If the dictionary contains a key, the corresponding vuale of the key is deleted and returned. if the key does not exist and the default value is not given, a keyerror exception is thrown;
11. the adict. setdefault (key, default = None) method is similar to the set () method, but if the Key does not exist in the dictionary, adict [key] = default assigns a value to it;
12. adict. update (bdict) adds the key-value pairs of the dictionary bdict to the dictionary adict.
VII. dictionary traversal
1. traverse the dictionary key (key)
The code is as follows:
For key in adict. keys (): print key
2. traverse the dictionary value (value)
The code is as follows:
For value in adict. values (): print value
3. Traverse dictionary items (elements)
The code is as follows:
For item in adict. items (): print item
4. traverse the dictionary's key-value
The code is as follows:
For item, value in adict. items (): print 'key = % s, value = % s' % (item, value) or for item, value in adict. iteritems (): print 'key = % s, value = % s' % (item, value)
Note: for item, value in adict. items (): print 'key = % s', 'value = % s', % (item, value) is incorrect.
8. precautions for using dictionaries
1. one key cannot correspond to multiple values;
2. keys must be Hashable.