python day 15 data type compositions - Dictionary

A mapping
1. Concept with mathematics
2. Mapping is a "key - the value" correspondence relationship, i.e., the correspondence relationship between the data and the index
3. Mapping, everywhere. For example, the name of the school - correspondence - Beijing Institute of Electronic Technology; address - correspondence - Fu Feng Road, No. 7, and so on. In fact, as long as the data link exists, it can exist mappings.
4. Sequence types, there are also mapped: the position index of the element constituting the mapping relationship.
Second, the dictionary
1. It is a new form of data organization and expression. New: Users can customize pairs.
2. Dictionary type really like to create a dictionary, like, when used, is also the same as it did in the dictionary
3. Create dictionary: {} or dict ()
. 4. Key-value pairs expressed in colon. Key-value pairs separated by commas
5. Dictionary Index [key],
6. Note: The dictionary index cancel the default index, that is, dict does not exist [0] returns the first key-value pair is
7. Note: {} generated both dictionaries, you can generate the collection! If there is a key-value pairs, that dictionary. There can not be a collection of key-value pairs in the dictionary can not have non-pairs.
Third, functions, methods
1. In operation, the key operation as a whole against normally object.
2. In operation, the general parameters of the "key", since the key is known to the user, and the value is generally unknown.
3. Add or modify elements Dictionary [Key] = value
. 4. Del dictionary [k] k Delete key corresponding to the key
5. Whether K in dictionary k key value in dictionary is not feasible in the dictionary
6. Dictionart.keys () returns a dictionary of all the "key"
7. Dictionary.values () returns the dictionary all the "value"
8. Dictionary.items () returns a dictionary of all "key on the"
9. Note: dict_keys returned ([]) may be traversed, but can not do the type of operation list
10. D.get (k (, default)) If k key exists, it returns a corresponding value, and if not, return parameter default
. 11. D.pop (k, (default)) supra, k takes a value corresponding to
12. D.popitem () randomly taken from the dictionary of a key, and returns the tuple form
13. D.clear
14. Len (d) Returns the number of key-value pairs
Fourth, the main application scenario
1. In fact, a combination of data types, is to better representation of life in complex data, and easy to manipulate. The dictionaries and maps, can be said to be everywhere
2. Dictionary traversal:
for i in the Dictionary ():
i equal to each key
remember use .keys () .values () .items ( )

Guess you like

Origin www.cnblogs.com/cfqlovem-521/p/12109934.html