Collections Series Function Introduction

Source: Internet
Author: User

The collections module is a newly added feature after python2.7.3.


1.1. Counter (Counter)

Counter is a supplement to the dictionary type that is used to track the number of values

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

>>> Import collections>>> c1=collections. Counter (' asdasdf ') >>> print c1counter ({' A ': 2, ' s ': 2, ' d ': 2, ' F ': 1})


1.2, C.update (d): Add C, D two counters

>>> C = Collections. Counter (' aabc ') >>> ccounter ({' A ': 2, ' C ': 1, ' B ': 1}) >>> d=collections. Counter (' AaB ') >>> dcounter ({' A ': 2, ' B ': 1}) >>> C.update (d) >>> ccounter ({' A ': 4, ' B ': 2, ' C ': 1})


1.3 Clear () empty counter

>>> Ccounter ({' A ': 4, ' B ': 2, ' C ': 1}) >>> c.clear () >>> ccounter ()


2. Ordered dictionary (ordereddict):

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

We know that the dictionary is unordered, the list is ordered, and the Ordereddict method is to save the dictionary when the key is assigned to a list in order, such as List=[k1,k2,k3,...]. Finally, the value of the dictionary is called in the order of key.

O1 = collections. Ordereddict () o1[' k1 '] = 1o1[' K2 '] = 2o1[' K3 '] = 3>>> o1ordereddict ([' K1 ', 1), (' K2 ', 2), (' K3 ', 3)])


3. Default dictionary (defaultdict): Sets a default type for values in the dictionary, which can be a list, a tuple, or a dictionary

For example:

>>> my_dict = collections.defaultdict (list) >>> my_dict[' K1 '].append (1) >>> My_ Dictdefaultdict (<type ' list ';, {' K1 ': [1]})

This is because the default values are set to list, so the Append method can be used later to assign values to the list


Default Dictionary 2: The above example can also be written like this

>>> dic = {}>>> dic[' k1 '] = []>>> dic[' K1 '].append (1) >>> dic{' K1 ': [1]}












This article is from the "Zengestudy" blog, make sure to keep this source http://zengestudy.blog.51cto.com/1702365/1821985

Collections Series Function Introduction

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.