0. Description
Still is to summarize the word image type and collection type according to the thought of several sequence types described earlier, that is to do an introduction first, then discuss the available operators, factory functions, built-in functions and methods.
1. Mapping Type: Dictionary
The mapping type is called a hash table, and the Dictionary object in Python is the hash type, that is, it stores the data in such a way that it is afetch key, an operation called a hash function on the key, and, based on the result of the calculation, a choice to store the value of the object in an address of the data structure. . Any value stored in the address depends on its key, and because of this randomness, the values in the hash table are not sequential, so the elements in the Dictionary object in Python are also unordered.
Here is the basic operation of the dictionary:
(1) Creating dictionaries and assigning values to dictionaries
The main methods are as follows:
>>> fdict = Dict ([' Xpleaf ', ' Boy '], [' Cl ', ' Girl ']) >>> fdict{' xpleaf ': ' Boy ', ' cl ': ' Girl '}
>>> fdict = {}.fromkeys (' xpleaf ', ' cl '), ' lovings ') >>> fdict{' xpleaf ': ' Lovings ', ' cl ': ' Lovings '} >>> edict = {}.fromkeys (' xpleaf ', ' cl ') >>> edict{' xpleaf ': None, ' CL ': none}
(2) Accessing values in a dictionary
>>> fdict = {' xpleaf ': ' Boy ', ' cl ': ' Girl '}>>>-key in Fdict.keys (): ... print key, Fdict[key] ... xp Leaf Boycl Girl
>>> fdict = {' xpleaf ': ' Boy ', ' cl ': ' Girl '}>>>-key in Fdict: ... print key, Fdict[key] ... xpleaf bo YCL Girl
>>> fdict = {' xpleaf ': ' Boy ', ' cl ': ' Girl '}>>> fdict.has_key (' xpleaf ') true>>> ' xpleaf ' in Fdicttrue>>> ' Clyyh ' in Fdictfalse
(3) Updating the dictionary
You can update existing key-value pairs or add new key-value pairs:
>>> fdict = {' host ': ' Xpleaf ', ' Port ': 80}>>> fdict[' port '] = 3389>>> fdict[' ip '] = ' 121.42.147 .131 ' >>> print ' Host:% (host) s, Port:% (port) s, IP:% (IP) s '% fdicthost:xpleaf, port:3389, ip:121.42.147.131
(4) Delete dictionary elements and dictionaries
>>> fdict = {' host ': ' Xpleaf ', ' Port ': ' IP ': ' 121.42.147.131 '}>>> del fdict[' Port '] #删除一个键值对 >> ;> fdict.clear () #删除字典中所有的键值对 >>> del fdict #删除整个字典
3. Mapping type Operators
In addition to not supporting stitching and repetition, dictionaries can work with all standard type operators.
(1) Standard type operator
Standard type operators have been introduced in the Python review and Collation 2:python objects, which are also suitable for dictionary types, mainly:>, <, = =, and and, as follows:
>>> dict1 = {' abc ': 123}>>> dict2 = {' abc ': 456}>>> dict1 < dict2true>>> Dict1 = = d Ict2false
(2) Mapping type operator
For a sequence type, an index is used to make a unique parameter or subscript to get the value of a sequence, but for a dictionary, it is a key query, and the key is a parameter, not an index. The ' [] ' operator allows you to find an element in a dictionary, or to assign a value to an element.
Of course, you can also use Has_key ().
This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1753670
Python Review and Collation 5: images and collective types