Python3_ use dict and set

dict

Python has a built-in dictionary: the support of dict, the full name of dict is dictionary, also called map in other languages, uses key-value (key-value) storage, and has extremely fast search speed.

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

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

Given a name, to find the corresponding score, you must first find the corresponding position in the names, and then extract the corresponding score from the scores. The longer the list, the longer the time.

If it is implemented with dict, only a "name"-"grade" comparison table is needed, and the results are directly searched according to the name. No matter how large the table is, the search speed will not slow down. Write a dict in Python as follows:

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

Why is dict lookup so fast? Because the implementation principle of dict is the same as that of 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 the element 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 index table of the dictionary (such as the radical table), and then directly turn to the page to find the word. This lookup is very fast no matter which word is looked for and does not slow down as the size of the dictionary increases.

Dict is the second implementation method. Given a name, for example 'Michael', dict can directly calculate Michaelthe "page number" of the corresponding storage score internally, that is 95, the memory address where this number is stored, and directly retrieve it, so the speed is very fast.

As you can guess, in this key-value storage method, when you put it in, you must calculate the storage location of the value according to the key, so that you can get the value directly according to the key when you take it.

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

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

Since a key can only correspond to one value, if you put a value into a key multiple times, the later 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: 'Thomas'

To avoid the error that the key does not exist, there are two ways, one is by injudging whether the key exists:

>>> 'Thomas' in d
False

The second is through the get method provided by dict. If the key does not exist, it can return None, or the value specified by itself:

>>> d.get('Thomas')
>>> d.get('Thomas', -1)
-1

Note: NonePython's interactive command line does not display results when returning.

To delete a key, use the pop(key)method, and the corresponding value is also deleted from the dict:

>>> d.pop('Bob')
75
>>> d
{'Michael': 95, 'Tracy': 85}

It is important to note that the order in which the dict is stored has nothing to do with the order in which the keys are placed.

Compared with list, dict has the following characteristics:

  1. The speed of searching and inserting is extremely fast, and it will not slow down as the key increases;
  2. It takes up a lot of memory and wastes a lot of memory.

And list is the opposite:

  1. The time to find and insert increases with the number of elements;
  2. Small footprint and little wasted memory.

So, dict is a way to trade space for time.

Dict can be used in many places where high-speed lookup is required. It is almost ubiquitous in Python code. It is very important to use dict correctly. The first thing to keep in mind is that the key of dict must be an immutable object .

This is because the dict calculates the storage location of the value according to the key. If the result obtained by calculating the same key each time is different, the interior of the dict will be completely confused. This algorithm for calculating the position by the key is called the hash algorithm (Hash).

To ensure the correctness of the hash, the object used as the key cannot be changed. In Python, strings, integers, etc. are immutable, so they can safely be used as keys. The list is mutable and cannot be used as a key:

>>> key = [1, 2, 3]
>>> d[key] = 'a list'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

 

set

Similar to dict, set is also a collection of keys, but does not store value. Since keys cannot be repeated, there are no repeated keys in the set.

To create a set, provide a list as the input set:

>>> s = set([1, 2, 3])
>>> s
{1, 2, 3}

Note that the incoming parameter [1, 2, 3]is a list, and the display {1, 2, 3}only tells you that there are 3 elements 1, 2, and 3 inside the set, and the order of display does not mean that the set is ordered. .

Duplicate elements are automatically filtered in the set:

>>> s = set([1, 1, 2, 2, 3, 3])
>>> s
{1, 2, 3}

add(key)Elements can be added to the set through methods, which can be added repeatedly, but have no effect:

>>> s.add(4)
>>> s
{1, 2, 3, 4}
>>> s.add(4)
>>> s
{1, 2, 3, 4}

remove(key)Elements can be removed by methods:

>>> s.remove(4)
>>> s
{1, 2, 3}

A set can be regarded as a collection of unordered and non-repetitive elements in a mathematical sense. Therefore, two sets can perform operations such as intersection and union in a mathematical sense:

>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4])
>>> s1 & s2
{2, 3}
>>> s1 | s2
{1, 2, 3, 4}

The only difference between set and dict is that the corresponding value is not stored. However, the principle of set is the same as that of dict, so it is also not possible to put mutable objects, because it is impossible to judge whether two mutable objects are equal, and it is impossible to guarantee set. Inside "there will be no repeating elements". Try putting the list into the set and see if an error will be reported.

Revisiting Immutable Objects

As we said above, str is an immutable object, and list is a mutable object.

For a mutable object, such as a list, when you operate on the list, the content inside the list will change, such as:

>>> a = ['c', 'b', 'a']
>>> a.sort()
>>> a
['a', 'b', 'c']

And for immutable objects, such as str, what about str:

>>> a = 'abc'
>>> a.replace('a', 'A')
'Abc'
>>> a
'abc'

Although the string has a replace()method, and it does change 'Abc', the variable ais still in the end 'abc'. How should we understand it?

Let's first change the code to the following:

>>> a = 'abc'
>>> b = a.replace('a', 'A')
>>> b
'Abc'
>>> a
'abc'

Always keep in mind that avariables 'abc'are string objects! Sometimes, we often say that athe content of the object is 'abc', but it actually means that ait is a variable itself, and the content of the object it points to is 'abc':

a-to-str

When we call a.replace('a', 'A')it, the calling method actually replaceacts on the string object 'abc', and although this method is named replace, it does not change 'abc'the content of the string. Instead, the replacemethod creates a new string 'Abc'and returns, bwhich is easy to understand if we point to the new string with a variable, the variable astill points to the old string 'abc', but the variable bpoints to the new string 'Abc':

a-b-to-2-strs

Therefore, for an immutable object, calling any method of the object itself will not change the content of the object itself. Instead, these methods create new objects and return them, thus guaranteeing that immutable objects themselves will always be immutable.

 summary

A dict using a key-value storage structure is very useful in Python. It is important to choose immutable objects as keys. The most commonly used keys are strings.

Although tuple is an immutable object, try putting the (1, 2, 3)sum (1, [2, 3])into a dict or set and interpret the result.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326831445&siteId=291194637