Dictionaries
The dictionary can be understood as an upgraded version of the list
List
list={"Dandan", "Zhangsan", "Wangwu"}
Dictionary
dic={' name ': ' Faint ', ' sex ': ' Man ', ' professional ': ' Computer Science and Technology '}
If you need to modify the "Zhangsan", you need to find its subscript, and the dictionary is based on the name (equivalent to the ID in HTML), subscript does not have a good memory dictionary and list can access multiple data. List query elements by subscript, dictionary query elements through: the previous value, such as the ' name ' in the above code, the ' Sex ' dictionary, each element consists of two parts preceded by the ID for the value dictionary common operations
print the elements in a dictionary like a list
Print (dic[' name '))
ModifyFind Assignment by ID
Temp=raw_input ("Please enter modified value")
dic[' name ']=temp
print (' Modified value is%s '%dic[' name ')
AddIf you assign a value to an ID that does not exist in a dictionary, this element is added to the dictionary
Vqq=raw_input ("Please enter QQ number")
dic[' qq ']=vqq
print ("Add QQ number is%s"%dic[' QQ '])
Image.png
RemoveDel Delete the specified element delete the entire dictionary (cannot be accessed after deletion) clear () clears the entire dictionary and can be accessed after deletion
Deletes the specified element
Del Delete the entire dictionary
Empty dictionary
Len () Look up the number of elements in the dictionary
An element is an ID and a value
Check the number of elements keys
View a list of all keys that contain a dictionary values
View list items that contain all values
Display the elements in a dictionary as a list Has_key
Dic.has_key (' key ') if the key is in the dictionary, returns True if there is no return false
Has_key traversal of the elements in the dictionary (similar to the For Loop traversal list) traversing all keys in the dictionary
Image.png Traversal of all the value in the dictionary
Image.png traversal of all items in the dictionary
Image.png traversal of all key-value in the dictionary (key-value pairs)
Image.png