A dictionary is another mutable container model and can store any type of object.each key value of the dictionary (key=>value) pairs with a colon (:), each pair is separated by a comma (,), the entire dictionary is included in curly braces ({})Medium:
DIC = {key1:value1, key2:value2}
The key must be unique, but the value does not have to be unique, and the value can take any data type, but the key must be immutable, such as a string, a number, or a tuple.
Ex
Id_name = {001:eric,002:lz,003:sky}
Take out the values in the dictionary:
< Span style= "Font-size:12px;background-color:rgb (255,255,255);" > id_name[003] # Put the corresponding key in square brackets
< Span style= "Font-size:12px;background-color:rgb (255,255,255);" >
is output
< Span style= "Font-size:12px;background-color:rgb (255,255,255);" >
< Span style= "Font-size:12px;background-color:rgb (255,255,255);" > New content is added to the dictionary by adding new key/value pairs, modified and deleted as follows:
< Span style= "Font-size:12px;background-color:rgb (255,255,255);" >
id_name = {"001": "Eric", "002": "LZ", "003": "Sky"}
id_name["004"] = "Hugo" &NB Sp #增加
Print (id_name)
id_name["002"] = "acer" &NBS P #修改
Print (id_name)
{' 001 ': ' Eric ', ' 004 ': ' Hugo ', ' 003 ': ' Sky ', ' 002 ': ' LZ '}
{' 001 ': ' Eric ', ' 004 ': ' Hugo ', ' 003 ': ' Sky ', ' 002 ': ' Acer '}
< Span style= "Font-size:12px;background-color:rgb (255,255,255);" > can delete a single element can also empty the dictionary, empty only one operation, show Delete a dictionary with the del command
[] #删除键是 entry for "002". Clear () #清空字典
Features of the dictionary key
A dictionary value can take any Python object without restriction, either as a standard object or as a user-defined one, but not a key.
Two important points:
1) The same key is not allowed to appear two times. When created, if the same key is assigned a value of two times, the latter value is remembered;
Id_name = {"001": "Eric", "002": "LZ", "003": "Sky"}
Id_name[] = id_name[] = (id_name)
{' 002 ': ' LZ ', ' 004 ': ' Hugo ', ' 003 ': ' Sky ', ' 001 ': ' Eric '}
{' 002 ': ' Red ', ' 004 ': ' Hugo ', ' 003 ': ' Sky ', ' 001 ': ' Eric '}
2) The key must be immutable, so it can be used as a number, a string or a tuple, so the list does not work;
The dictionary contains the following built-in functions:
CMP (DIRC1,DIRC2) |
Comparison of two dictionary elements
|
Len (DIRC1)
|
Computes the number of dictionary elements, that is, the total number of keys
|
STR (DIRC1)
|
Output dictionary printable string representation
|
Type (Valu)
|
Returns the type of the variable, which is the dictionary that returns the dictionary type |
The dictionary contains the following built-in methods:
D.clear () |
Delete all elements in a dictionary
|
D.copy ()
|
Returns a shallow copy of a dictionary |
D.fromkeys ([seq], "Val")
|
Creates a new dictionary with the elements in the sequence SEQ as keys, and Val is the initial value for all keys in the dictionary
|
D.get (Keys,default=none)
|
Returns the value of the specified key, or returns the default value if the value is not in the dictionary |
D.keys () |
Returns all keys in the dictionary as a list |
D.items ()
|
Returns an array of traversed (key, value) tuples as a list |
D.setdefault (Key,default=none)
|
Similar to get, but if the key does not add a key in the dictionary, the value is default
|
D.update (D2)
|
Update Dictionary d2 key-value pairs to dictionary D
|
D.values ()
|
Returns all values in the dictionary as a list
|
D.has_key (Key)
|
If the key returns true in the dictionary, it is not returned in the false,3.0 to cancel the |
This article is from the "LE" blog, please be sure to keep this source http://lz001.blog.51cto.com/8294567/1792974
Python Learning-dictionaries