Python Learning notes Dict, set

Source: Internet
Author: User
Tags stdin

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 = [957585]

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, such as ' Michael ', Dict can directly calculate Michael's corresponding storage Score "page number", that is, 95 of the memory address of the number stored, directly out, 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>"1in <module>KeyError:‘Thomas‘

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

>>> ‘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 corresponding value is also removed from dict using the Pop (key) 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:

* * Find and insert the speed is very fast, will not increase with the key increase;
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]>>> d[key‘a list‘call last):  "<stdin>"1in <module‘list‘
Set

Set and dict are similar 也是一组key的集合 , 但不存储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])>>> sset([123])

Note that the parameters passed in [1, 2, 3] are a list, and the displayed set ([1, 2, 3]) just tells you that this set has 3 elements in the inside of a single, and that the [] display does not indicate that this is a list.

Repeating elements are automatically filtered in set:

set([112233])>>> sset([123])

You can add elements to the set by using the Add (key) method, and you can add them repeatedly, but without effect:

>>> s.add(4)>>> sset([1234])>>> s.add(4)>>> sset([1234])

You can delete an element by using the Remove (key) method:

>>> s.remove(4)>>> sset([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([123set([234])>>> s1 & s2set([23])>>> s1 | s2set([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:

a‘abc‘a.replace(‘a‘‘A‘)‘Abc‘a‘abc‘

Although the string has a replace () method, it does change the ' ABC ', but the variable A is still ' 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 is a variable, and that ' abc ' is a String Object! Sometimes, we often say that the content of object A is ' abc ', but in fact it means that a is itself a variable, it points to the content of the object is ' abc ':

A-to-str

When we call A.replace (' A ', ' a '), the actual calling method replace is on the string object ' abc ', and this method, although named Replace, does not change the contents of the string ' abc '. Instead, the Replace method creates a new string ' abc ' and returns, if we point to the new string with variable B, it's easy to understand that variable a still points to the original string ' abc ', but the variable B points to the new string ' abc ':

A-b-to-2-strs

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.

Python Learning notes Dict, 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.