Home  >  Article  >  Backend Development  >  Understand the meaning of the dict function in Python in 3 minutes

Understand the meaning of the dict function in Python in 3 minutes

Tomorin
TomorinOriginal
2018-08-17 14:47:575538browse

Python has built-in dictionary: dict support, dictfull namedictionary, also called map in other languages, using key-value (key- value) storage, with extremely fast search speed.

For example, suppose you want to find the corresponding grades based on the names of classmates. If you use a list to implement it, you need two lists:

names = ['Michael', 'Bob', 'Tracy']
scores = [95, 75, 85]

Given a name, if you want to find the corresponding grades, just First find the corresponding position in names, and then retrieve the corresponding results from scores. The longer the list, the longer it will take.

If implemented with dict, only a "name"-"score" comparison table is needed, and the results can be directly searched based on the name. No matter how big the table is, the search speed will not slow down. . Use Python to write a dict as follows:

>>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
>>> d['Michael']
95

Why is dict search so fast? Because the implementation principle of dict is the same as looking up a dictionary. Suppose the dictionary contains 10,000 Chinese characters, and we want to look up a certain word. One way is to turn the dictionary back from the first page until we find the word we want. This method is to find elements in the list. The larger the list, the slower the search.

The second method is to first look up the page number corresponding to the word in the dictionary index table (such as the radical table), and then directly turn to the page to find the word. No matter which word you are looking for, this search speed is very fast and will not slow down as the size of the dictionary increases.

dict is the second implementation method. Given a name, such as 'Michael', dict can directly calculate the "page number" corresponding to Michael's score internally, which is the memory where the number 95 is stored. The address is taken out directly, so the speed is very fast.

As you can guess, with this key-value storage method, when you put it in, you must calculate the storage location of the value based on the key, so that when you retrieve it, you can directly get the value based on the key.

In addition to specifying the method during initialization, the method of putting data into dict can also be put in by key:

>>> d['Adam'] = 67
>>> d['Adam']
67

Since one key can only correspond to one value, one key can be entered multiple times. Put in value, and the subsequent value will flush out the previous value:

>>> d['Jack'] = 90
>>> d['Jack']
90
>>> d['Jack'] = 88
>>> d['Jack']
88

If the key does not exist, dict will report an error:

>>> d['Thomas']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: &#39;Thomas&#39;

To avoid the error that the key does not exist, there are two ways The first method is to determine whether the key exists through in:

>>> &#39;Thomas&#39; in d
False


The above is the detailed content of Understand the meaning of the dict function in Python in 3 minutes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn