Day3-python base class source code parsing--collection class

Source: Internet
Author: User

1. Counter (counter)

Counter is a supplement to the dictionary type that is used to track the number of occurrences of a value.

PS: With all the functions of the Dictionary + own function

We select some of the relatively common methods for example:

As we can see in the above example, the Counter method returns a dictionary that counts all the characters that appear in the string. Here we introduce the Update method, the Update method is to add the results of two statistics, and the dictionary update slightly different.

  

2. Ordered dictionary (ordereddict)

Orderddict is a supplement to the dictionary type, and he remembers the order in which the dictionary elements were added

We all know that dictionaries are inherently unordered, and it relies on the index of Key,value to match, so what is the principle of an ordered dictionary? Principle: dic = {' K2 ': 1, ' k1 ': 2},li = [' K1 ', ' K2 '], this dictionary maintains a key list internally.

We know from the above diagram that although the dictionaries we define are written sequentially from 1 to 8, they are not printed in the order we want them to be printed. The advantage of an ordered dictionary at this point is:

3. Default dictionary (defaultdict)

Defaultdict is a supplement to the type of dictionary, which defaults to a type for the value of the dictionary.

The following functions are implemented in code

有如下值集合 [ 11 , 22 , 33 , 44 , 55 , 66 , 77 , 88 , 99 , 90. ..],将所有大于  66 的值保存至字典的第一个key中,将小于  66 的值保存至第二个key的值中。 即: { ‘k1‘ : 大于 66 ‘k2‘ : 小于 66 }

看出神奇的地方了么?我们可以不需要在空字典中指定value的值,直接执行append,就可以向字典中插入值了,就是因为我们使用defauldict(list)方式定义了一个value值默认为list的字典。

Otherwise we're going to write this:

How to implement a common dictionary

4, can be named tuple (namedtuple)

Depending on nametuple, you can create a type that contains all the functions of a tuple and other features.

Mainly used for the representation of ' coordinates '. Use the following:

5. Bidirectional Queue (deque)

A thread-safe two-way queue: Two-way queue we can understand that the stack is connected to two stacks, and the queue's FIFO, the elements can be added or deleted from the queue at each end of the value. Although the list can actually fully implement this function, but the Python collections class is very intimate to these methods are summed up, crooked nuts is interesting ah ~ ~ ~





From for notes (Wiz)

Day3-python base class source code parsing--collection class

Related Article

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.