Dict
Python built-in dictionary: dict support, Dict full name dictionary, in other languages also known as map, using key-value (Key-value) storage, with a very fast search speed.
For example, suppose you want to find the corresponding score according to the name of the classmate, if you use list, you need two lists:
names = ['Michael'Bob'Tracy ' = [[+]
Given a name, to find the corresponding score, first to find the corresponding position in the names, and then remove the corresponding results from scores, the longer the list, the longer the time.
If implemented with Dict, only need a "name"-"score" of the table, directly based on the name of the results, no matter how large the table, the search speed will not be slow. Write a dict in Python as follows:
>>> d = {'Michael'Bob' Tracy'd[' >>>'Michael' ]
Why does Dict look so fast? Because the implementation principle of dict and look dictionary is the same. Suppose the dictionary contains 10,000 characters, we need to look up a 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 method, the larger the list, the slower the lookup.
The second method is to search the dictionary's index table (such as the Radicals) for the corresponding page number, and then go directly to the page, find the word. Whichever word you look for, this is very fast and does not slow down as the dictionary size increases.
Dict is the second implementation, given a name, for example ‘Michael‘
, dict in the internal can directly calculate the Michael
corresponding storage score "page", that is, the 95
number of memory address, directly removed, so the speed is very fast.
As you can guess, this key-value storage method, when put in, must be based on the key to calculate the storage location of value, so that the time can be obtained by key directly to the value.
The method of putting data into dict, in addition to being specified during initialization, can also be placed by key:
>>> d['Adam'>>> d['Adam ' ]
Since a key can only correspond to one value, the value is placed multiple times for a key, and the following values will flush the previous value out:
>>> d['Jack' >>> d['jack' >>> d['Jack'>>> d[' Jack ' ]
If key does not exist, Dict will get an error:
>>> d['Thomas']traceback (most recent): " <stdin>"1 in <module>'Thomas'
To avoid a key that does not exist, there are two ways to in
determine whether a key exists:
' Thomas ' inch Dfalse
The second is the method provided by Dict, get()
if key does not exist, it can be returned None
, or its own specified value:
>>> d.get('Thomas')>>> d.get(' Thomas ',-1)-1
Note: When you return None
, the interactive environment of Python does not display the results.
To delete a key, the pop(key)
corresponding value is also removed from the dict using the method:
>>> d.pop ('Bob')>>> d{' Michael' "Tracy"
It is important to note that the order of Dict internal storage is not related to the order in which key is placed.
Compared with list, Dict has the following features:
- The speed of finding and inserting is very fast and will not slow with the increase of key;
- It takes a lot of memory, and it wastes a lot of memory.
And the list is the opposite:
- The time to find and insert increases as the element increases;
- Small footprint and little wasted memory.
So, Dict is a way of exchanging space for time.
Dict can be used in many places where high-speed lookups are needed, almost everywhere in Python code, it is important to use dict correctly, and the first thing to keep in mind is that the Dict key must be an immutable object .
This is because Dict calculates the storage location of value based on key, and if each calculation of the same key results in a different result, the dict interior is completely chaotic. The algorithm for calculating the position by key is called the hash Algorithm (hash).
To ensure the correctness of the hash, the object as a key can not be changed. In Python, strings, integers, and so on are immutable, so you can safely use them as keys. The list is mutable and cannot be a key:
>>> key = [123]'a list'Traceback (most Recent: "<stdin>"1 in <module >'list'
Set
Set is similar to Dict and is a set of keys, but does not store value. Because key cannot be duplicated, there is no duplicate key in set.
To create a set, you need to provide a list as the input collection:
Set ([123])>>> s{123}
Note that the parameter passed in [1, 2, 3]
is a list, and the display {1, 2, 3}
only tells you that the set has 3 elements, the order of the display does not indicate that the set is ordered.
Repeating elements are automatically filtered in set:
Set ([112233])>>> s{1 2 3}
You can add(key)
add elements to a set by means of a method, and you can add them repeatedly, but with no effect:
>>> S.add (4)>>> s{1234} >>> S.add (4)>>> s{1234 }
remove(key)
You can delete an element by means of:
>>> S.remove (4)>>> s{123}
Set can be regarded as a set of unordered and non-repeating elements in mathematical sense, so two sets can do the intersection and set of mathematical meanings, and so on:
Set ([123]) Set ([234])>>> S1 & s2{23} >>> S1 | s2{1234}
The only difference between set and dict is that it does not store the corresponding value, but the set principle is the same as the dict, so it is also not possible to put mutable objects, because it is not possible to determine whether the two Mutable objects are equal, and there is no guarantee that there will be no duplicate elements inside the set. Try putting the list in set to see if it will give an error.
Re-discussing non-mutable objects
As we said above, Str is an immutable object, and list is a mutable object.
For mutable objects, such as list, to manipulate the list, the contents of the list will change, such as:
>>> a = ['C'b'a'] >>> a.sort ()>>> a['a' b ' ' C ']
For non-mutable objects, such as STR, we operate on STR:
' ABC ' >>> a.replace ('a'a')' ABC' >>> a'abc'
Although the string has a replace()
method, it does change ‘Abc‘
, but the variable is a
still at the end ‘abc‘
, how should it be understood?
Let's start by changing the code to the following:
' ABC '>>> b = a.replace ('a'a')> >> b'abc'>>> a'ABC '
Always keep in mind that a
it is a variable, but a ‘abc‘
string Object! Sometimes, we often say that the object's a
content is ‘abc‘
, but actually refers to, a
itself is a variable, it points to the content of the object is ‘abc‘
:
┌───┐ ┌───────┐│ a │─────────────────>│ ‘abc‘ │└───┘ └───────┘
When we call a.replace(‘a‘, ‘A‘)
, the actual invocation method replace
is on the string object ‘abc‘
, and although the method is called, it replace
does not change ‘abc‘
the contents of the string. Instead, the replace
method creates a new string ‘Abc‘
and returns, and if we point to the new string with a variable, b
it's easy to understand that the variable a
still points to the original string ‘abc‘
, but the variable points to the b
new string ‘Abc‘
:
┌───┐ ┌───────┐│ a │─────────────────>│ ‘abc‘ │└───┘ └───────┘┌───┐ ┌───────┐│ b │─────────────────>│ ‘Abc‘ │└───┘ └───────┘
Therefore, for an immutable object, any method that invokes the object itself does not change the contents of the object itself. Instead, these methods create a new object and return it, ensuring that the immutable object itself is always immutable.
Summary
Dict using the Key-value storage structure is very useful in python, it is important to choose an immutable object as key, and the most commonly used key is a string.
9 Python Basics-Using dict and set