One, what is a dictionary?
Dictionaries are the only mapping types in the Python language.
The hash value (key, key) and the object (value) in the mapped type object are one-to-many relationships and are often considered to be variable hash tables.
A Dictionary object is variable and is a container type that can store any number of Python objects, which can also include other container types.
The difference between a dictionary type and a sequence type:
1. Access to and access to data vary in different ways.
2. The sequence type is indexed in numeric order only with numeric type keys (from the beginning of the sequence);
3. Mapping types can be used as keys for other object types (such as numbers, strings, ganso, usually strings), and the key of the type of the sequence is different, the key of the mapping types is 4. or indirectly, associated with storing data values.
5. The data in the mapping type is unordered. This is not the same as the sequence type, and the sequence types are in numerical order.
6. The mapping type is directly "mapped" to the value using the key.
Dictionaries are one of the most powerful data types in Python.
Ii. How to create a dictionary and assign a value to a dictionary
Simply put, a dictionary is a collection of key-value pairs wrapped in braces. (Key-value pairs are also called items)
General form:
Copy Code code as follows:
Adict = {}
Adict = {key1:value2, key2:value2, ...}
or with the Dict () function, e.g., adict = Dict () or adict = Dict ([' X ', 1],[' Y ', 2]) is that right? Adict = Dict ([' X ', 1],[' Y ', 2]). Keyword parameters to create a dictionary, such as: adict= dict (name= ' Allen ', age= ' 40′)
or using the Fromkeys () method, such as, adict = {}.fromkeys (' x ', ' Y '),-1) The value of the dictionary created is the same, and if not, the default is None.
Characteristics:
1, the key and value by colon ":" separate;
2, items and items with commas "," separate;
3, the key in the dictionary must be unique, and the value can not be unique.
Copy Code code 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 form, such as: ' Age ': ' 040′ without ' age ': 040
Basic operation of the dictionary
1, how to access the value in the dictionary?
Adict[key] Returns the value of the key, and if the key is not in the dictionary, a keyerror is thrown.
2, how to check the key is in the dictionary?
A, Has_key () method in the form of: Adict.haskey (' name ') has –>true, no –>false
b, in, and not in like: ' Name ' in Adict have –>true, no –>false
3, how to update the dictionary?
A, add a data item (new Element) or a key value pair
Adict[new_key] = value to add an item
b, update one 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 key key/del adict delete entire dictionary
Adict.pop (key) deletes key keys and returns the value of key
Iv. Mapping Type Operators
Standard type operator (+,-,*,<,>,<=,>=,==,!=,and,or, not)
A, dictionaries do not support stitching and repeat operators (+,*)
b, the dictionary comparison operation
Compare the length of the dictionary to the number of elements in the dictionary
Key comparison
Value comparison
Example:
Copy Code code as follows:
Adict = {}
bdict = {' name ': ' Allen ', ' Age ': ' 40′}
CMP (adict, bdict) <–>-1 or >–>1 or ==–>0
V. Mapping-related functions
1, Len () back to the length of the dictionary
2. Hash () returns the hash value of an object that can be used to determine whether an object can be used as a key to a dictionary
3, Dict () factory function, used to create a dictionary
Vi. Methods of the dictionary
1, Adict.keys () returns a list containing all keys of the dictionary;
2, Adict.values () returns a list containing all the value of the dictionary;
3, Adict.items () returns a list containing all the (key, value) Ganso;
4, adict.clear () Delete all the items or elements in the dictionary;
5, Adict.copy () returns a copy of a shallow copy of the dictionary;
6, Adict.fromkeys (seq, val=none) to create and return a new dictionary, with the elements in SEQ to do the key of the dictionary, Val do all the keys in the dictionary corresponding to the initial value (the default is None);
7, Adict.get (key, default = None) returns the value of the key in the dictionary, if the key does not exist in the dictionary, returns the value of default (default is None);
8, Adict.has_key (key) If the key is in the dictionary, returns True, otherwise returns false. Now with in, not in;
9, Adict.iteritems (), Adict.iterkeys (), Adict.itervalues () and their corresponding non iterative methods, the difference is that they return an iteration, rather than a list;
10, Adict.pop (Key[,default]) and get methods are similar. If a key exists in the dictionary, delete and return the Vuale of the key, and throw a Keyerror exception if the key does not exist and the value of default is not given;
11, Adict.setdefault (key, Default=none) and set () method is similar, but if the dictionary does not exist key key, by adict[key] = default for it to assign value;
12, Adict.update (bdict) adds the dictionary bdict key-value pairs to the dictionary adict.
Seven, the dictionary traversal
1, traversing the Dictionary key (key)
Copy Code code as follows:
For key in Adict.keys ():p rint key
2, traversing the dictionary value (values)
Copy Code code as follows:
For value in Adict.values (): Print value
3, traversing the Dictionary of the item (element)
Copy Code code as follows:
For item in Adict.items ():p rint Item
4, traversing the key-value of the dictionary
Copy Code code 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) This is a wrong notation.
Eight, the use of the dictionary considerations
1, can not allow a key corresponding to multiple values;
2, the key must be hash.