Python uses dict and set

Source: Internet
Author: User

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‘]scores = [95, 75, 85]

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‘: 95, ‘Bob‘: 75, ‘Tracy‘: 85}>>> d[‘Michael‘]95

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 index table (such as the radical table) the corresponding page number, and then directly to the page, find the word, regardless of which word, this search speed is very fast, not with the increase in the dictionary size and slow.

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‘] = 67>>> d[‘Adam‘]67

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‘] = 90>>> d[‘Jack‘]90>>> d[‘Jack‘] = 88>>> d[‘Jack‘]88

If key does not exist, Dict will get an error:

>>> d[‘Thomas‘]Traceback (most recent call last):  File "<stdin>", line 1, in <module>KeyError: ‘Thomas‘

To avoid a key that does not exist, there are two ways to in determine whether a key exists:

>>> ‘Thomas‘ in dFalse

The second is the Get method provided by Dict, if key does not exist, you can return none, or the value you specified:

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

Note: When none is returned, the interactive command line 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‘)75>>> d{‘Michael‘: 95, ‘Tracy‘: 85}

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:

    1. The speed of finding and inserting is very fast and will not increase with the increase of key;
    2. It takes a lot of memory, and it wastes a lot of memory.

And the list is the opposite:

    1. The time to find and insert increases as the element increases;
    2. 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 = [1, 2, 3]>>> d[key] = ‘a list‘Traceback (most recent call last):  File "<stdin>", line 1, in <module>TypeError: unhashable type: ‘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:

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

Note that the passed in parameter [1, 2, 3] is a list, and the display set([1, 2, 3]) only tells you that this set has 3 elements in the inside of the "X", which shows [] does not indicate that this is a list.

Repeating elements are automatically filtered in set:

>>> s = set([1, 1, 2, 2, 3, 3])>>> sset([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)>>> sset([1, 2, 3, 4])>>> s.add(4)>>> sset([1, 2, 3, 4])

remove(key)You can delete an element by means of:

>>> s.remove(4)>>> sset([1, 2, 3])

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:

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

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:

>>> a = ‘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:

>>> a = ‘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‘ :

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‘ :

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.

Although a tuple is an immutable object, try putting (1, 2, 3) and putting (1, [2, 3]) in a dict or set and interpreting the results.

Python uses dict and set

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.