- Dict (dictionary) is a collection of unordered objects, consisting of key-value pairs, which are obtained by reading a key, with variable, unordered, heterogeneous, and nested properties.
- Dict initialization
1, directly using the dictionary format
2, using Dict (), [note]: The key does not add '
3, the use of tuple (tuple)
4, use Dict.fromkeys (), return the dictionary, the method has two parameters, parameter 1 is a list of keys, parameter 2 is the initial value of these keys, the default is empty
- Dict Add, Delete, modify
1, key if not present add key-value, if present, modify key-value
2. Delete the specified key-value pair
3. Delete the specified key-value, return the value of the delete key, or none if the specified key does not exist
4. Delete key-value of entire dictionary
5. Update the dictionary with Dict.update (), update if the key is the same, add if the key does not exist
6. Use Dict.setdefault () to set the default value of the key and return the value. If the key exists, returns the original value of the key, or, if the key does not exist, adds a key-value, which returns the value
- Dict's Query
1. Specify key
2. Using the Dict.items () method, returns a list of all key-value pairs (items), with key-value pairs expressed in tuple (tuple) Form
3. Use the Dict.keys () method to return the list of all keys
4. Use the Dict.values () method to return a list of values for all keys
5. Use the For loop to traverse the dictionary
6, use the Dict.get () method, if the specified key exists, then return the wisdom of the key, if not present, return the value given in the parameter (the second argument)
7. Use Len () to return the number of key-value pairs in the dictionary
8. Use Dict.has_key () if the key exists returns true if the key does not exist return False (python3.x no Dict.has_key ())
- Dict Other common methods
1, using copy.copy () shallow copy
2. Use Copy.deepcopy () deep copy
3, using dict.copy () shallow copy
Python Dictionay (dictionary) Basic usage