What is a dictionary?
A dictionary is the only type of mapping in the Python language.
A hash value (key, key) and a pointer to an object (values, value) are a one-to-many relationship that is often considered a mutable hash table in the mapping type object.
The Dictionary object is mutable, which is a container type that can store any number of Python objects, including other container types.
The difference between a dictionary type and a sequence type:
1. Access to and access to data differs in different ways.
2. The sequence type only uses the key of the numeric type (indexed in numerical order from the beginning of the sequence);
3. The mapping type can be used as a key for other object types (such as numbers, strings, ganso, generally with strings as keys), and the key of the sequence type is different, the mapping type of the key is straight 4. Associate or indirectly with the stored data value.
5. The data in the mapping type is unordered. This is not the same as the sequence type, and the sequence type is numerically ordered.
6. The mapping type is directly "mapped" to a value with a 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
In a nutshell, a dictionary is a collection of key-value pairs wrapped in curly braces. (Key-value pairs are also referred to as items)
General form:
The code is as follows:
Adict = {}
Adict = {key1:value2, key2:value2, ...}
or with the Dict () function, such as, adict = Dict () or adict = Dict (([' X ', 1],[' Y ', 2])) Do you write that right? Adict = Dict ([' X ', 1],[' Y ', 2]). Keyword parameter creation dictionary, such as: adict= dict (name= ' Allen ', age= ' 40′)
or with 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 the value with the colon ":" separate;
2, the term and the item with a comma "," separate;
3. The keys in the dictionary must be unique, and the values may not be unique.
The code is as follows:
adict = {' name ': ' Allen ', ' name ': ' Lucy ', ' Age ': ' 40′} with 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
Iii. basic operation of the dictionary
1. How do I access the values in the dictionary?
Adict[key] Returns the value of key key, if key is not in the dictionary, a keyerror is raised.
2. How to check if key is in the dictionary?
A, Has_key () method shape: Adict.haskey (' name ') has –>true, no –>false
b, in, No in shape: ' Name ' in adict with –>true, no –>false
3, how to update the dictionary?
A, add a data item (new Element) or key-value pair
Adict[new_key] = value form adds an item
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 key key item/del adict delete entire dictionary
Adict.pop (key) deletes the key keys and returns the value corresponding to key
Iv. Mapping Type Operators
Standard type operator (+,-,*,<,>,<=,>=,==,!=,and,or, not)
A, dictionaries do not support splicing and repeating 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:
The code is as follows:
Adict = {}
bdict = {' name ': ' Allen ', ' Age ': ' 40′}
CMP (adict, bdict) <–>-1 or >–>1 or ==–>0
V. Mapping-related functions
1, Len () returns the length of the dictionary
2, hash () returns the hash value of the object, which can be used to determine whether an object can be used as a dictionary key
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 (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 a dictionary;
6, Adict.fromkeys (seq, val=none) creates and returns a new dictionary, with the element in SEQ making the dictionary key, and Val does the initial value corresponding to all the keys in the dictionary (default is None);
7, Adict.get (key, default = None) returns the value corresponding to the key in the dictionary, and returns the value of default if the key does not exist in the dictionary (default defaults to none);
8, Adict.has_key (key) If key is in the dictionary, returns True, otherwise 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]) is similar to the Get method. If there is a key in the dictionary, delete and return the key corresponding to the Vuale; if the key does not exist and the default value is not given, the keyerror exception is thrown;
11, Adict.setdefault (key, Default=none) and set () method similar, but if the dictionary does not exist in the key key, by adict[key] = default for it assignment;
12, Adict.update (bdict) adds the dictionary bdict key-value pairs to the dictionary adict.
Vii. the traversal of a dictionary
1. Traverse the dictionary key (key)
The code is as follows:
For key in Adict.keys ():p rint key
2. The value of the traversal dictionary
The code is as follows:
For value in Adict.values (): Print value
3. Traversing dictionary entries (elements)
The code is as follows:
For item in Adict.items ():p rint Item
4, traverse the dictionary 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) This notation is wrong
Viii. Considerations for using dictionaries
1, can not allow a key to correspond to multiple values;
2, the key must be hashed.