# Python----Collections Module

Source: Internet
Author: User

` fromCollectionsImportNamedtuple,deque,defaultdict,ordereddict,counterImportQueue#can name tuples, namedtuplePoint = Namedtuple (' Point',['x','y']) P= Point ()Print(P.X,P.Y)#Output Results: 1 2#deque Dual-ended queueA = Deque (['a','b','C','D']) A.appendleft ('x')Print(a)#Output Result: deque ([' X ', ' A ', ' B ', ' C ', ' d '])A.append ('y')Print(a)#output: Deque ([' X ', ' A ', ' B ', ' C ', ' d ', ' Y '])b =A.pop ()Print(b)#output Result: Yc =A.popleft ()Print(c)#output Result: xA.insert (2,3)#not recommended for this .Print(a)#output: deque ([' A ', ' B ', 3, ' C ', ' d '])#Queue FIFO FirstQ =queue. Queue () Q.put (10) Q.put (5) Q.put (4) Q.put (3) Q.put (2)Print(q)Print(Q.get ())#TenPrint(Q.get ())#5Print(Q.qsize ())#defaultdictDIC = Defaultdict (Lambda:'N /A') dic['K1'] ='ABC'Print(dic['K2'])#The output is n/a . The default value is returned when key does not exist. #Ordereddict ordered dictionaryD = ordereddict ([('a', 1), ('b', 2), ('C', 3)])Print(List (D.keys ()))#Counter The purpose of this class is used to track the number of occurrences of a value, which is an unordered container, stored as a dictionary#where the element is a key, its count as value. CC = Counter ('ABCDADFKDFJ')Print(CC)#output: Counter ({' d ': 3, ' F ': 2, ' a ': 2, ' C ': 1, ' K ': 1, ' J ': 1, ' B ': 1})#detailed description of the address: http://wwww.cnblogs.com/Eva-J/articles/7291842.html`

Python----Collections Module

Related Keywords:
Related Article

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.