This article extracts from the MU class net "The introduction of Python"
1. Dict characteristics
Dict is denoted by curly braces {} and then Key:value, written out. The last comma of a key:value can be omitted.
①,dict search speed is fast , regardless of whether the dict has 10 elements or 100,000 elements, the search speed is the same . The search speed of the list decreases as the element increases.
The disadvantage of Dict is that it takes up a lot of memory, and it wastes much content , the list is the opposite, the memory is small, but the search speed is slow
②,dict to find value by key, so key cannot be duplicated, and value can be repeated
③,dict stored "key:value" is unordered , that is, not available index number slices.
Python's basic types, such as strings, integers, and floating-point numbers, are immutable and can be used as keys. But the list is mutable and cannot be a key.
The lookup table for "name"-"score" is shown in dict as follows:
D = {
' Adam ': Up, #key: value
' Lisa ': 85,
' Bart ': 59
}
We call the name key, the corresponding result is called value,dict is the key to find value.
2. Visit dict
Create a dict that represents the correspondence between the name and the score:
D = {
' Adam ': 95,
' Lisa ': 85,
' Bart ': 59
}
Use D[key] to find the corresponding value, which is similar to the list, where thelist must return the corresponding element using the index, and Dict uses key:
23:30:12
Note: The value of dict is accessed by key, and Dict returns the corresponding value as long as the key exists. If key does not exist, it will directly error: Keyerror.
There are two ways to avoid keyerror:
First, determine whether the key exists, with the in operator:
If ' Paul ' in D:
Print d[' Paul '
If ' Paul ' is not present, the IF statement evaluates to False, and the print d[' Paul ' is not executed naturally, thus avoiding the error.
The second is to use the dict itself to provide a get method, when the key does not exist, return to none:
>>> print d.get (' Bart ')
59
>>> print d.get (' Paul ')
None
The dict is mutable and you can add new Key-value to the dict at any time. For example, there are dict:
D = {
' Adam ': 95,
' Lisa ': 85,
' Bart ': 59
}
To add a new classmate ' Paul ' score 72, use the assignment statement:
d[' Paul '] = 72
Then look at the contents of Dict:
>>> Print D
{' Lisa ': $, ' Paul ': $, ' Adam ': Up, ' Bart ': 59}
If the key already exists, the assignment replaces the original value with the new value:
>>> d[' Bart '] = 60
>>> Print D
{' Lisa ': $, ' Paul ': $, ' Adam ': Up, ' Bart ': 60}
>>> d = {' Adam ': $, ' Lisa ': $, ' Bart ': 59}
>>> for key in D:
Print key, '-', D[key]
Lisa-85
Adam-95
Bart-59
Ⅱ,values ()/Itervalues () method: Returns the value of Dict
>>> d = {' Adam ': $, ' Lisa ': $, ' Bart ': 59}
>>> Print d.values ()
[85, 95, 59]
>>> for V in D.values ():
Print V
85
95
59
Replace the values () method with the itervalues () method, which is exactly the same as the iteration effect. and itervalues ()
Ⅲ,items ()/Iteritems () method: Returns the key and value of Dict
The value returned by the items () method of the Dict object:
>>> d = {' Adam ': $, ' Lisa ': $, ' Bart ': ' D.items ' >>> print ' [(' Lisa ', ' $ '), (' Adam ', '), (' Bart '), 59)]
As you can see, the items () method converts the Dict object to a list that contains a tuple and iterates over the list to obtain both key and value:
>>> for key, value in D.items (): ... print key, ': ', Value ... lisa:85 adam:95 bart:59
Like the values () has a itervalues (), items () also have a corresponding iteritems (), Iteritems () does not convert the dict to a list, but is constantly given in the iterative process Tuple, so Iteritems () does not occupy additional memory.
python--about Dict