Python mapping type Dictionary dict operation definition
Python Mapping Type Dictionary basics: the dictionary is represented by curly braces {}, which is the most flexible built-in data type in Python. It is an unordered collection that accesses values by key and cannot be indexed.
Creation and use of dictionaries
The composition of a dictionary: A dictionary consists of curly braces {} to contain its data, the curly braces contain the key and its corresponding value, and a pair of keys and values becomes an item. Keys and values are separated by a colon: a comma, a number between items and items. An empty dictionary is a curly brace that does not contain any items, such as {}, which is an empty dictionary.
For example, create a student file dictionary with name, age, and Gender: The Dict Dictionary is created as follows
Why is the key of the Dictionary (key), and the value of the item, can be clearly understood from the picture. The large enclosing {} number contains these elements as dictionaries. Note: The key to the dictionary must be an immutable data type, and if you use tuples for keys, you must ensure that the pairs in the tuple are immutable types as well. mutable data types cannot be keyed.
Of course, the method of creating a dictionary is not the only one, like the Dict () method is also very useful, in a more in-depth study of these techniques will be described in the creation of methods.
How to access values in a dictionary
Dictionaries are unordered and cannot be looked up in the same way as index subscripts. It has its own unique approach, which is to find it by key.
>>>dangan[' xingming ']
' Lili '
>>> variable name [key name]
Value
Dictionary Add method
Dictionaries are added in different ways: Dictionary variable name [newly added key name] = value corresponding to new key
>>> dangan[' aihao '] = ' Changge '
>>>dangan
{' Aihao ': ' Changge ', ' nianling ': ' xingming ': ' Lili ', ' Xingbie ': ' Nan '}
Dictionary Value Modification methods
Dictionary modification format: dictionary variable name [to modify the value corresponding to the key name] = new value
>>>dangan[' nianling '] = 19
>>>dangan
{' Aihao ': ' Changge ', ' nianling ': ' xingming ': ' Lili ', ' Xingbie ': ' Nan '}
The dictionary addition and modification methods are actually the same. The key name to which the new value is to be modified, if it exists, is the equivalent of adding an action if it does not exist.
Dictionary Delete method
There are three common ways to delete a dictionary, and the function is different. The following is simply the format of these methods
1, del method: delete the corresponding value of the key, Del space variable name [key name], if the only write variable name is to delete this dictionary
2, Clear method: Empty the dictionary content, variable name. Clear ()
3, Python pop method: Delete the value corresponding to the key, but it will be the value of the output should be deleted after the
The dictionary items () method and the Iteritems () method are the built-in functions of the Python dictionary that return python lists and iterators, together with a look at how the dictionary items () and Iteritems () work.
Role
the items method of the Python dictionary functions : It is possible to return all items in the dictionary as a list. Because the dictionary is unordered, all items in the dictionary are returned with the items method, and there is no order.
the Iteritems method of the Python dictionary works: it is roughly the same as the items method, except that its return value is not a list but an iterator.
Call format
The dictionary items () and iteritems () are functions that call the standard format the same as other function formats: variables. Method ()
Operation method
Dictionary items () How to:
>>> x = {' title ': ' Python Web site ', ' url ': ' Www.iplaypy.com '}
>>> X.items ()
[(' url ', ' www.iplaypy.com '), (' title ', ' Python web site ')]
As you can see from the results, theitems () method is to add each item in the dictionary as a tuple, adding it to a list, forming a new list container . If necessary, the returned result can be assigned to a new variable, which will be a list data type.
>>> A=x.items ()
>>> A
[(' url ', ' www.iplaypy.com '), (' title ', ' Python web site ')]
>>> type (a)
<type ' list ' >
dict Iteritems () operation Method :
>>> f = x.iteritems ()
>>> F
<dictionary-itemiterator Object at 0xb74d5e3c>
>>> type (f)
<type ' Dictionary-itemiterator ' > #字典项的迭代器
>>> list (f)
[(' url ', ' www.iplaypy.com '), (' title ', ' Python web site ')]
The dictionary. Iteritems () method works best when iterative results are required, and is highly productive.
Python mapping type Dictionary dict operation definition