Third Day---collections 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.

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 complement to dictionary types, which can be output sequentially by adding a dictionary element.

General dictionary: unordered, it relies on the index between Key,value to match.

1dict={2     '1':'test1',3     '2':'test2',4     '3':'test3',5     '4':'test4',6     '5':'Test5',7     '6':'Test6'8 }9  forKvinchDict.items ():Ten     PrintKv

Ordered dictionaries: The dictionary automatically maintains a key list internally.

Import Collectionsdict1=collections. Ordereddict () for in range (1,7):    dict1[str (lines)]='test%s '%(lines) for in Dict1.items ():    Print k,v

3. Default dictionary (defaultdict)

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

有如下值集合 [ 11 , 22 , 33 , 44 , 55 , 66 , 77 , 88 , 99 , 90. ..],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中。 即: { ‘k1‘ : 大于 66 , ‘k2‘ : 小于 66 }Common methods:
Dic={}li=[11,22,33,44,55,66,77,88,99] forIteminchLi:ifitem>66:        if 'K2'inchDic.keys (): dic['K2'].append (item)Else: dic['K2']=[item,]Else:        if 'K1' inchDic.keys (): dic['K1'].append (item)Else: dic['K1']=[item

Implemented with the default dictionary

1 ImportCollections2value=[11,22,33,44,55,66,77,88,99]3dict1=collections.defaultdict (list)4  forList1inchValue:5     iflist1<=66:6dict1['K1'].append (List1)7     Else:8dict1['K2'].append (List1)9 PrintDict1

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:

1 ImportCollections as Coll2>>> Mytuple=coll.namedtuple ('Mytuple',['x','y'])3>>> N=mytuple ()4>>>N5Mytuple (X=1, y=2)6>>>n.x718>>>N.Y92

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.

A=coll.deque ()
A.append (1)
A.append (2)
A.append (3)
A.pop ()
3
A.popleft ()
1

Third Day---collections class

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.