Python3 Module Collections

Source: Internet
Author: User

1. Counter (counter)

Counter is a dictionary method used to track the number of occurrences of a value, with all the functions and functions of a dictionary.

>>> fromCollectionsImportCounter>>> D2 =Count>>> D2 = Counter ('abbcccddd')#Count of Values>>>Print(D2) Counter ({'D': 3,'C': 3,'b': 2,'a': 1})>>>Print(Type (D2))<class 'collections. Counter'>>>> D2 = Counter ('abbcccddd')>>> forIinchD2.elements ():#count Element iterator...Print(i) ... bbadddccc>>> D2.clear ()#empty Elements>>>Print(D2) Counter ()>>> d3 = D2.copy ()#Copy>>>D3counter ({'D': 3,'C': 3,'b': 2,'a': 1})>>> D2.update ('ddffffff')#Update>>>D2counter ({'F': 6,'D': 5,'C': 3,'b': 2,'a': 1})>>> D2.subtract ('fffff')#subtract the number of elements>>>D2counter ({'D': 5,'C': 3,'b': 2,'a': 1,'F': 1})>>> D4 = D2.pop ('F')#extracting key from Dict>>>D41>>>D2counter ({'D': 5,'C': 3,'b': 2,'a': 1})

2. Ordered dictionary (ordereddict)

Ordereddict is a dictionary supplement that remembers the order in which the dictionary elements are added.

>>> fromCollectionsImportordereddict>>> Dic1 = ordereddict ({'K1':'v1','K2':'v2','K4':'V4','K3':'v3'})>>> Dic1#to create a dictionary with a sequenceOrdereddict ([('K4','V4'), ('K1','v1'), ('K2','v2'), ('K3','v3')])>>>Print(Type (DIC1))<class 'collections. Ordereddict'>>>> Dic1.popitem ()#default fetch last to key('K3','v3')>>>dic1ordereddict ([('K4','V4'), ('K1','v1'), ('K2','v2')])>>> Dic1.move_to_end ('K4')#moves the specified key to the last>>>dic1ordereddict ([('K1','v1'), ('K2','v2'), ('K4','V4')])

3. Default dictionary (defaultdict)

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

>>> fromCollectionsImportdefaultdict>>> Dict1 = defaultdict (list)#Create a default dictionary>>>Dict1defaultdict (<class 'List'>, {})>>>Print(Type (DICT1))<class 'collections.defaultdict'>>>> forIinchRange (20):#Example...ifi%2 = =0: ... dict1['K1'].append (i) ...Else: ... dict1['K2'].append (i) ...>>>Dict1defaultdict (<class 'List', {'K1': [0, 2, 4, 6, 8, 10, 12, 14, 16, 18],'K2': [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]})

4, can be named tuple (namedtuple)

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

>>> fromCollectionsImportNamedtuple>>> tup1 = Namedtuple ('Tup1',['x','y','Z','u'])#to create a named tuple>>> obj = tup1 (111,22,333,4444)#Creating Objects>>>Print(obj.x)#the value of the output object sequence111>>>Print(OBJ.Y)22>>>Print(OBJ.Z)333>>>Print(OBJ.U)4444

Python3 Module Collections

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.