Data structure with hash table
Dictionaries and sets
I will explain the hash table in detail in the database section later. At present, we only need to know that the hash table can be quickly searched.
Let me give an example. The time it takes to search 10,000 times in the list (data structure that does not contain the hash table) and set (the data structure that contains the hash table):
The result of running this program is:
You will find that it takes a long time to search when the data structure does not contain a hash table, and when the data structure contains a hash table, the search can be completed in almost no time.
This difference becomes more obvious when the amount of data becomes larger.
So when we perform large-scale data search, we should use data structure contains hash tables.
One requirement for using a hash table is: there can only be one of each element. If the content to be searched contains duplicate elements, then the hash table cannot be used to speed up the search.
For example, you can use a hash table to find specific elements in 1, 2, 3, 4, 5, but you cannot find specific elements in 1, 1, 2, 2, 3 because they contain duplicate elements. This is reflected in both sets and dictionaries: sets do not allow duplicate elements, and dictionaries do not allow duplicate keys.
This feature of the hash table is very dangerous. If the content you need to find contains duplicate elements, it may cause errors in the search results.
set
The meaning of set is derived from mathematics and refers to an unordered list without repetition.
We can create a new collection in the following way.
We can also convert a list into a collection.
You will find that the repeated elements have been deleted from the set.
The set uses a hash table index, so the set is unordered, and we cannot index the set:
Set is not used much in python, it is mainly used to remove redundant elements in the list:
dictionary
Dictionaries are used a lot in python. Because the hash table index is used, the dictionary can be looked up quickly.
As shown in the code block above, we have created a dictionary.
Please note that the keys in the dictionary are unique, which means you should not operate as follows(Repeated assignments will be overwritten, you should be extra careful about this, because it may lead to incorrect program results)
The keys of the dictionary must be hashable. Any non-hashable data structure mentioned before cannot be used as the key of the dictionary:
The hashable tuple can be used as a dictionary key:
Modify dictionary
Modifying the dictionary is very simple, we only need to overwrite the contents of the corresponding key of the dictionary:
Create empty dictionary
Creating an empty dictionary is very simple, just use dict() to create it.
Query dictionary
You only need to enter the key to query the dictionary. The above is the query method.
Delete a key of the dictionary
The above is how to delete a key in the dictionary.
Delete the entire dictionary
The above code will delete a dictionary. If you have deleted this dictionary, you will get an error when you call this dictionary again.
Statistics
Start time of this page: December 19, 2021
Completion time of this page: January 6, 2022
Last updated