Tag: Represents the HTTP one list post list reference number default
A dictionary of Key-value data types.
The dictionary is unordered, the key must be unique, but the value is not necessary, and the value can take any data type, but the key must be immutable, such as a string, a number, or a tuple.
Common operations for dictionaries
Create
Directly create
Dict1 = {' Student1 ': ' On ', ' Color ': [' red ', ' white '], ' Guangdong ': {' Guangzhou ': [' Haizhu ', ' Tianhe '], ' Shenzhen ': [' Nanshan ', ' Futian '}}
Create by using the Dict function
#通过list创建 >>> student = [(' Student1 ', ' Student2 '),]>>> student_dict = dict (student) > >> student_dict{' student1 ': ' student_dict ', ' student2 ': ' The ' #通过关键字参数创建 '} >>> dict Student2= ' "student3= ') >>> student_dict{' student1 ': ' The ', ' student2 ': ' In ', ' student3 ': ' 03 '}
Additional
>>> student_dict{' student1 ': '}>>> ', ' student2 ': ' In ', ' student3 ': ' ['] ', ' [] [] []] = ' student_dict{' >>> ' student1 ': ' The ', ' student2 ': ' student4 ', ' student3 ': ' The ' ', ' ' The ' ": ' 04 '}
Modify
>>> student_dict{' student1 ': ' student4 ', ' student2 ': '}>>> ', ' student3 ': ' The ' ', ' The ' ' ": ' The ' ', ' [] dict[' student4 ' = ' 004 ' >>> student_dict{' student1 ': ' A ', ' student2 ': ' In ', ' student3 ': ' The ', ' student4 ': ' 004 ‘}
Delete
>>> student_dict{' student1 ': ' student4 ', ' student2 ': ' In ', ' student3 ': ' In the ', ' 004 ': ' A. '}>>> Student_ Dict.popdel student_dict[' student3 ']>>> student_dict{' student1 ': ' ', ' student2 ': ' 02 '} >>> student_dict.popitem() #随机删除 (' Student2 ', ' in ') >>> student_dict{' student1 ': ' 01 ' }
Find
' Student1 ' in Student_dict True>>> student_dict.getstudent_dict[' student1 ']' ">>> student_dict.get (' Student5 ') >>> student_dict[' student5 '] #如果一个key不存在, error, get method does not exist, only return None
>>> student_dict{' student1 ': ' student4 ', ' student2 ': '}>>> ', ' student3 ': ' The ' ', ' The ' ' ": ' The ' ', ' [] Dict.keys() Dict_keys ([' Student1 ', ' student2 ', ' student3 ', ' student4 ']) >>> student_dict. Values() dict_values ([' "], ' student_dict ', '" ' "', '" ') ',' ['] ' dict_items ' (' Student1 ', ' Student2 '), (' Student3 ', ' ""), (' Student4 ', ' ") ') >>> type (Student_dict.items ()) < Class ' Dict_items '>>>> type (student_dict) <class ' dict ' >
#嵌套字典查找 >>> dict1 = {' Student1 ': ' On ', ' Color ': [' red ', ' white '], ' Guangdong ': {' Guangzhou ': [' Haizhu ', ' Tianhe '] , ' Shenzhen ': [' Nanshan ', ' Futian ']}}>>> dict1[' Guangdong ' [' Guangzhou '][0] ' Haizhu '
Update
>>> student_dict2 = {' student5 ': 'to ', 'student2 ': ' 002 '}>>> student_dict{' Student1 ': ' Student2 ': '}>>> ', ' student3 ': ' In the ', ' student4 ': ' In ', ' and ', 'and ' ', ' [] STUDENT_DICT2) ' student2 ': ' 002'student5 ':' in '}
Traverse
#方法1for key in Info: print (Key,info[key]) #方法2for k,v in Info.items (): #会先把dict转成list, try not to print (K,V) when the data is large
Dictionary-related functions
Serial Number |
Functions and descriptions |
Example |
1 |
Len (dict) Calculates the number of dictionary elements, that is, the total number of keys. |
>>>={' Name ':' Runoob ',' age ':7,' Class ':' First '}>>> len(dict)3 |
2 |
STR (DICT) The output dictionary, expressed as a printable string. |
>>>={' Name ':' Runoob ',' age ':7,' Class ':' First '}>>> str(dict)"{' Name ': ' Runoob ', ' Class ': ' First ', ' Age ': 7}" |
3 |
Type (variable) Returns the type of the variable entered and returns the dictionary type if the variable is a dictionary. |
>>> dict = {' Name ': ' Runoob ', ' age ': 7 , ' Class ': ' first '}>>> type(dict)< class ' Dict '> |
Built-in methods for dictionaries
Serial Number |
Functions and descriptions |
1 |
Radiansdict.clear () Delete all elements in a dictionary |
2 |
Radiansdict.copy () Returns a shallow copy of a dictionary |
3 |
Radiansdict.fromkeys () Create a new dictionary with the keys to the dictionary in sequence seq, Val is the initial value corresponding to all keys in the dictionary |
4 |
Radiansdict.get (Key, Default=none) Returns the value of the specified key if the value does not return the default value in the dictionary |
5 |
Key in Dict Returns False if the key returns true in the dictionary Dict |
6 |
Radiansdict.items () Returns an array of traversed (key, value) tuples as a list |
7 |
Radiansdict.keys () Returns a dictionary of all keys in a list |
8 |
Radiansdict.setdefault (Key, Default=none) Similar to get (), but if the key does not exist in the dictionary, the key is added and the value is set to default |
9 |
Radiansdict.update (DICT2) Update the key/value pairs of the dictionary dict2 to the Dict |
10 |
Radiansdict.values () Returns all values in the dictionary as a list |
11 |
Pop (Key[,default]) Deletes the value of the dictionary given key key, and returns the value to be deleted. The key value must be given. Otherwise, the default value is returned. |
12 |
Popitem () Randomly returns and deletes a pair of keys and values in the dictionary (typically delete the end pair). |
Reference Link: http://www.runoob.com/python3/python3-dictionary.html
Reference Link: http://www.cnblogs.com/alex3714/articles/5717620.html
Python Basics 7-Dictionaries