dict

Python has built-in support for dictionaries: dict, which stands for dictionary and is also called map in other languages. It uses key-value storage for extremely fast lookups.

For example, if you want to find the corresponding grade based on the name of the classmate, if you use list, you need two lists:

names = ['Michael'.'Bob'.'Tracy']
scores = [95.75.85]
Copy the code

To find scores from scores given a name, locate scores from names and retrieve scores from scores. The longer the list, the longer it takes.

Dict only requires a comparison table of “name” and “score” to search results by name. No matter how big the table is, the search speed will not be slower. Write a dict in Python like this:

>>> d = {'Michael': 95.'Bob': 75.'Tracy': 85}
>>> d['Michael']
95
Copy the code

Why are dict lookups so fast? Dict works the same way as looking up a dictionary. Let’s say the dictionary contains 10,000 characters and we want to look up a word, one way is to scroll back from the first page of the dictionary until we find the word we want, and that’s the way you look up elements in a list, the bigger the list, the slower the search.

The second method is to look up the corresponding page number of the word in a dictionary index (such as a radical table) and then go directly to that page to find the word. No matter what word you are looking for, this lookup is very fast and does not slow down as the dictionary size increases.

Dict is the second implementation. Given a name, such as ‘Michael’, dict internally calculates Michael’s “page number”, which is the memory address where the number 95 is stored, so it is very fast.

In this key-value storage mode, the location of the value must be calculated according to the key when the value is put in. In this way, the value can be directly obtained according to the key when the value is fetched.

insert

Methods for putting data into dict, in addition to being specified at initialization, can also be put by key:

>>> d['Adam'] = 67
>>> d['Adam']
67
Copy the code

change

If you add a value to a key multiple times, the subsequent value will flush out the previous value:

>>> d['Jack'] = 90
>>> d['Jack']
90
>>> d['Jack'] = 88
>>> d['Jack']
88
Copy the code

Dict reports an error if the key is not present:

>>> d['Thomas']
Traceback (most recent call last):
  File "<stdin>", line 1.in <module>
KeyError: 'Thomas'
Copy the code

To find the

There are two ways to avoid the error that the key does not exist. First, check whether the key exists by using in:

>>> 'Thomas' in d
False
Copy the code

The other is the get() method provided by dict, which returns None or its own value if the key does not exist:

>>> d.get('Thomas')
>>> d.get('Thomas'.- 1)
- 1
Copy the code

Note: Python’s interactive environment does not display results when None is returned.

delete

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

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

It’s important to note that the order in which dict is stored internally has no relationship to the order in which keys are placed.

And list

Compared to lists, dict has the following characteristics:

1. The search and insert speed is extremely fast and will not slow down with the increase of keys; 2. It takes up a lot of memory and wastes a lot of memory.Copy the code

List is the opposite:

1. Search and insert times increase as the number of elements increases; 2. Occupies little space, and wastes little memory.Copy the code

So dict is a way to trade space for time.

Dict can be used in many places that require high-speed look-ups, almost everywhere in Python code, and it’s important to use dict correctly. The first thing to remember is that dict keys must be immutable objects.

This is because dict calculates where a value is stored based on a key, and if the same key is computed with different results every time, dict is completely out of order. The algorithm that calculates the position by key is called a Hash algorithm.

To ensure that the hash is correct, the key object cannot be changed. In Python, strings, integers, and so on are immutable and can be safely used as keys. 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'
Copy the code

set

A set, like a dict, is a set of keys, but does not store values. Because the key cannot be repeated, there is no duplicate key in set.

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

>>> s = set([1.2.3])
>>> s
{1.2.3}
Copy the code

Note that the argument [1, 2, 3] is a list, and the display {1, 2, 3} only tells you that the set has 1, 2, 3 elements inside it. The display order does not indicate that the set is ordered.

Duplicate elements are automatically filtered in set:

>>> s = set([1.1.2.2.3.3])
>>> s
{1.2.3}
Copy the code

insert

We can add elements to a set using the add(key) method, which can be added repeatedly but has no effect:

>>> s.add(4)
>>> s
{1.2.3.4}
>>> s.add(4)
>>> s
{1.2.3.4}
Copy the code

delete

Remove elements with the remove(key) method:

>>> s.remove(4)
>>> s
{1.2.3}
Copy the code

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

>>> s1 = set([1.2.3])
>>> s2 = set([2.3.4])
>>> s1 & s2
{2.3}
>>> s1 | s2
{1.2.3.4}
Copy the code

The only difference between a set and a dict is that it does not store the corresponding value. However, a set works in the same way as a dict, so it cannot be put into a mutable object. Because it is impossible to determine whether two mutable objects are equal, there is no guarantee that there will be “no duplicate elements” in a set.

Discuss immutable objects again

So we saw above that STR is an immutable object, and list is a mutable object.

For mutable objects, such as list, the contents of the list change when you operate on it, for example:

>>> a = ['c'.'b'.'a']
>>> a.sort()
>>> a
['a'.'b'.'c']
Copy the code

For immutable objects, such as STR, how about operating on STR:

>>> a = 'abc'
>>> a.replace('a'.'A')
'Abc'
>>> a
'abc'
Copy the code

Even though the string has a replace() method, which does produce ‘Abc’, the variable a still ends up with ‘Abc’.

>>> a = 'abc'
>>> b = a.replace('a'.'A')
>>> b
'Abc'
>>> a
'abc'
Copy the code

Always remember that a is a variable and ‘ABC’ is a string object! Sometimes we say that the content of object A is’ ABC ‘, but what we really mean is that A is itself a variable and the content of the object it refers to is’ ABC ‘:

┌ ─ ─ ─ ┐ ┌ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ a ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ > │'abc'│ └ ─ ─ ─ ┘ └ ─ ─ ─ ─ ─ ─ ─ ┘Copy the code

When a.place (‘a’, ‘a’) is called, the method replace is actually called on the string object ‘ABC ‘, which, despite its name, does not change the contents of the string’ ABC ‘. Instead, the replace method creates a new string ‘Abc’ and returns it, which is easier to understand if we use variable B to point to the new string. Variable A still points to the original string ‘Abc’, but variable B points to the new string ‘Abc’ :

┌ ─ ─ ─ ┐ ┌ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ a ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ > │'abc'│ └ ─ ─ ─ ┘ └ ─ ─ ─ ─ ─ ─ ─ ┘ ┌ ─ ─ ─ ┐ ┌ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ b ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ > │'Abc'│ └ ─ ─ ─ ┘ └ ─ ─ ─ ─ ─ ─ ─ ┘Copy the code

Therefore, for immutable objects, calling any method of the object itself does not change the contents of the object itself. Instead, these methods create new objects and return them, thus ensuring that immutable objects themselves are always immutable.

summary

Dict that uses key-value structures is useful in Python, and it’s important to choose immutable objects as keys, the most commonly used keys being strings.