Getting started with Python (11/18)
Section 11 Dictionary
Hello everyone! Today we are learning Python's dictionary. It is still one of the four major data structures of Python, and it is also a very special data type.
1、 dictionary
A dictionary (dict) is the only sequence of mapping types in the Python language. The value of the dictionary object is variable. It is a container type that can store any number of Python objects, and can also nest other objects.
1、 Compared with other sequence types of data, the dictionary has different ways of accessing and accessing data.
2、 Other sequence types are ordered and actually imply numeric keys (index labels).
3、 Dictionaries are unordered, with keys and values appearing in pairs, relying on keys to index values.
4、 The keys of the dictionary cannot be changed. You can use numbers, strings, and ancestors. Generally, strings are used as keys.
5、 The key and value have a mapping relationship, because the uniqueness of the key makes the objects in the dictionary not repeated.
2、 Create a dictionary
A dictionary (dict) is a collection of "key-value pairs" composed of keys and values.
**Description: **
1、 The key and value are separated by a colon (:), each key-value pair is separated by a comma, and the whole is placed in curly braces {}.
2、 The key must be unique and cannot be changed. Therefore, any type that supports in-situ change cannot be used as a key, such as list, dict, and set, but can be numbers, strings, and tuples.
3、 The value does not have to be unique, it can take any data type, even nested dictionary.
4、 The dictionary is not a sequence, it looks disordered inside. But it supports sorting.
3、 Addition, deletion, modification, and query of dictionary: [addition]
We can add key-value pairs to the dictionary directly or through the dict.setdefault() method.
**Description: **
1、 You can directly add new key-value pairs to the dictionary directly by assigning new keys.
2、 Use the setdefault() method to add new key-value pairs to the dictionary and return the currently added value.
3、 If the key added by the dict.setdefault() method already exists, it will not be appended, only the value corresponding to the key will be returned
4、 Addition, deletion, modification, and search of dictionary: [Delete]
Using the del, pop() and clear() methods, one or all key-value pairs can be deleted.
**Description: **
1、 The del method can directly delete the key-value pair corresponding to the key by specifying the key.
2、 The pop() method can also delete the key-value pair of the specified key in the dictionary and return the value corresponding to the key.
3、 The dictionary will clear all key-value pairs through the clear() method.
5、 Addition, deletion, modification, and search of dictionary: 【modification】
By specifying the key or the update() function, you can directly and indirectly modify a key-value pair.
**Description: **
1、 The dictionary can directly modify the corresponding value by specifying the key method.
2、 Use the update() function to update the value of a new dictionary to the specified dictionary. When encountering the same key, modify its value, and append if it is different.
3、 You can also use a list of tuples as a parameter of the update() function to update a specified dictionary.
6、 Addition, deletion, modification and search of dictionary: [Check 1]
The dictionary no longer uses the subscript index to access, but uses the key to access the corresponding value.
**Description: **
1、 You cannot use the subscript index method to access the values in the dictionary.
2、 The key and the value have a one-to-one correspondence, and the corresponding value can be accessed through the key.
3、 The same key is not allowed in the dictionary. If it is added repeatedly, only the following key value will be kept in the memory.
7、 Addition, deletion, modification, and check of dictionary: [Check 2]
Use the dict.keys(), dict.values(), and dict.items() methods to query all keys, values, and key-value pairs respectively.
**Description: **
1、 The dict.keys() method returns a doct_keys object that contains a list of all the keys of the dictionary.
2、 The dict.values() method returns a doct_values object that contains a list of all the values of the dictionary.
3、 The dict.items() method returns a doct_items object that contains a list of tuples consisting of all the key-value pairs of the dictionary.
8、 Dictionary iteration
Using the for key in dict iteration statement, you can easily traverse all key-value pairs in a dictionary.
9、 Dictionary merge
In python3, use the update() function to easily merge two dictionaries.
The previous introduction already knows that if the key value in dict1 exists in dict2, the value in dict2 will be used instead of the value in dict1 instead of appending. In this way, when two dicts are merged, a perfect superposition can be formed without duplicate key-value pairs.
10、 Sorting of the dictionary
In python, dictionaries seem to be unordered, and sorting can be achieved using the sorted() function.
The function sorted() sorts all iterable objects.
sorted(req,key=lambda x: x) has two parameters, the first req is a sortable object, the second key is the target to be sorted, here is a lambda expression.
11、 Commonly used built-in functions or methods of dictionaries
**Python dictionary has the following commonly used built-in functions and methods. Please note that the functions or methods we sometimes find on the Internet will report errors when using them, which is probably a version problem.
len(dict) #Returns the length of the dictionary (the number of key-value pairs)
dict.clear() #Delete all elements in the dictionary
dict.copy() #Return a shallow copy of a dictionary
dict.get(key, default) #Return the value of the specified key, if the key does not exist, return the default value
dict.setdefault(key, val) #Similar to get(), but if the key does not exist, add the key-value pair
dict.items() #Return a traversable (key, value) tuple array as a list
dict.keys() #Returns a list of all keys of a dictionary
dict.update(dict2) #Update the key/value pairs of dictionary dict2 to dict
dict.values() #Return all values in the dictionary as a list
summary
In this lesson, we introduced in detail the creation and use of a dictionary (dict).
Preview
In the next lesson, we will continue to introduce the Python data structure: Sets, which is one of the most commonly used data structures in Python and the last data structure we will focus on. The most commonly used scenarios for sets are "de-duplication" and set operations (union, intersection and difference), which are very similar to the set concepts we learned in junior high school mathematics.
Recommended Posts