Baptism of the soul, cultivation of python (8)-efficient dictionary, baptism of python

Source: Internet
Author: User
Tags python web crawler

Baptism of the soul, cultivation of python (8)-efficient dictionary, baptism of python
The core of python-Dictionary (dict)

1. What is a dictionary?

Have you used dictionaries in the student age? You can use a dictionary to find the relevant information, such as its part of speech, meaning, group words, sentences, and under what circumstances.

The principle of this dictionary in python is the same. Of course, this dictionary in python is certainly not used to query words, but to query the data we need. However, if you really want to use the python dictionary to search for a word, you can use the python web crawler to search for the word. Besides, you can query a lot of things.

The feature Number of the dictionary is curly braces {}

2. Functions of the dictionary:

Used to associate an array or classify data.

3. multiple ways to create a dictionary:

A: create a direct value assignment.

The dictionary is different from the previous list, tuples, and sets,['X'] is the key, the Chinese name is the key, connected with [:], [1] is the value, and the Chinese name is the value. These three are combined as an element of the dictionary.. Generally, an element in the dictionary is a key-value pair.

B: Create using factory functions dict and keyword parameters (What are keyword parameters will be mentioned later)

C: Create from the Binary Group list

D: Create dict and zip in combination

The zip function can combine two objects:

 

Note the differences between the zip functions in python2 and python3. The above two figures have already shown

 

E: created through dict. fromkeys ()

Pay attention to the usage of the fromkeys () method.

 

3. Dictionary Method

Python2:

Python3:

Because the difference between the two is indeed quite big, combined with the actual, I will compromise the extraction of a few commonly used:

Clear, copy, fromkeys, get, items, keys, pop, popitem, update, setdefault, values

Clear: clear all. It is almost the same as the clear method in the previous chapter and will not be parsed.

Copy: copy, which is almost the same as the copy method of the previous chapter.

Fromkeys: generate a dictionary object, which has been explained during dictionary creation.

Get: Return Value Based on the given key

 

NOTE: For the get method, the first parameter is the dictionary key, and the second parameter is optional. If the dictionary key to be accessed does not exist, nothing is returned by default. If the second parameter is added and the key does not exist, the given second parameter is returned, but must comply with the python syntax rules.

Items: returns all key-value pairs of the dictionary in the form of a list.

Keys: returns all keys of the dictionary in the form of a list, which is not displayed after the same principle as items.

Values: returns all dictionary values in the form of a list, which is not displayed after the same principle as items.

Pop: for a given key, delete the corresponding key-Value Pair and return

Popitem: sort by dictionary priority (or random) to delete and return

Update: The update method is almost the same as the update method of the set. Use another dictionary to update the dictionary.

 

Setdefault: add an element and set a default value.

 

However, if the key already exists in the dictionary when the key is set, it will remain unchanged:

4. The dictionary can be accessed and modified like the index in the list, but the key used by the dictionary is not the index,No index for the dictionary

 

5. The dictionary is a mutable object and can be modified at the original location, that is, the ID remains unchanged. I believe it is not necessary to demonstrate it.

6. The dictionary can be used for variable unpacking:

However, the number of elements must match. Otherwise, an error is returned.

 

Note: The dictionary key must be a hash value.

What is hash? Here we have to mention a built-in function-hash ()

[Hash] You can understand it as a basis for search and have a unique meaning, because keys in the dictionary cannot correspond to multiple values, otherwise, a value cannot correspond to multiple keys. The hash function is used to determine whether the object or element can be hashed. If it cannot be hashed, an error is returned. The above list is not hashable, because the list is an object that can be modified in the original place, for example, you introduced me to a person today, and then you went back to him to pay back the money, and you found that he was easy to handle. What you remembered before he could no longer help you find him, so you can't find your money. (This is a logical loophole. You just need to understand the principle .). Based on this analogy, you should know that the object that can be modified as the key cannot have a unique value, right? Therefore, the list cannot be used as a key, nor can it be hashed. The above error is easy to understand.

 

The dictionary itself cannot be hashed, And the dictionary key can be hashed. Because the dictionary also supports the original Modification

 

You should ask why the dictionary key must be Hash-able? A: When you look up a word in the dictionary used in the student age, you need to get its pronunciation or radicals, then, you can follow the pinyin or radical table on the front page of the dictionary to find out where the word is. The key here is like the pronunciation or radicals you know, using it as the basis will soon search for this key in the dictionary key, and then immediately return the value corresponding to this key, while in big data processing, generally, the dictionary and the hash algorithm are used to store data, because the hash algorithm can efficiently and quickly find the location of the key. The dictionary obtains the key and then returns the corresponding value. Unlike the list and tuples, the list and metadata are ordered. The default search method for the list and metadata is to start from the index value of 0. In other words, the query starts from the beginning to the end, when the data size is large, the processing speed is very slow, and the list is saved in seconds using a dictionary.

The actual example is that web pages are continuously crawled by crawlers every day, such as Baidu and other search engines, combined with some algorithms such as breeze algorithms (SEO should be familiar with these), the storage keywords are stored in the dictionary and stored on the server. Then, when users use keywords to search for content, the baidu search engine will quickly respond, and immediately feed back all the information stored in the dictionary containing this keyword to the user, the real implementation is probably just a blink of an eye (is there a feeling that I am writing a novel/manual funny?). The principle is like this. In fact, the search engine is not just as simple as I said, there are also a lot of complicated algorithms that can be combined for implementation, otherwise it is impossible to achieve such a fast speed. Okay, let's not talk about it anymore. In short, you know that the dictionary is powerful.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.