Python Basic Collections Module

Source: Internet
Author: User

Collections Module

The Collections module provides several additional data types based on the built-in data types (dict, list, set, tuple): Chainmap, Counter, deque, Defaultdict, Namedtuple and Ordereddict and so on.

1.namedtuple: Generates a tuple subclass that can access the content of an element by using a name
2.deque: Double-ended queue to quickly append and eject objects from the other side
3.Counter: Counter, mainly used to count
4.OrderedDict: Ordered Dictionary
5.defaultdict: Dictionary with default values

Nametuple:
1 """2 Namedtuple is a function that creates a custom tuple object, specifies the number of tuple elements, and can refer to an element of a tuple using a property rather than an index. 3 4 In this way, we can easily define a data type with namedtuple, which has the invariant of tuple and can be referenced according to the attribute, which is very convenient to use. 5 """6  fromCollectionsImportNamedtuple7Point= Namedtuple (" Point",['x','y'])8P=point ()9 Print(P.X,P.Y)Ten Print(Isinstance (p,point)) One Print(Isinstance (P,tuple))
View CodeDeque
1 """2 when using the list to store data, accessing the elements by index is fast, but inserting and deleting elements is slow because list is linear and the data is large, and insertions and deletions are inefficient. 3 4 Deque is a two-way list for efficient insert and delete operations, suitable for queues and stacks:5 In addition to implementing the list's Append () and pop (), deque supports Appendleft () and Popleft (), which allows you to add or remove elements to the head very efficiently. 6 """7  fromCollectionsImportdeque8Q=deque (['a','b','C'])9Q.append ('x')TenQ.appendleft ('Z') One Print(q) A Q.popleft () - Print(q)
View CodeCounter:
1 """2 counter is a simple counter, for example, the number of statistical characters that appear:3 counter is actually a subclass of Dict.4 """5 6  fromCollectionsImportCounter7C=Counter ()8  forChinch "acceptable":9C[ch]=c[ch]+1Ten Print(c)
View CodeOrdereddict:
1 """2 when using Dict, key is unordered. We cannot determine the order of key when we iterate over the dict. 3 4 If you want to keep the key in order, you can use Ordereddict:5 ordereddict keys are sorted in the order in which they are inserted, not the key itself:6 7 """8  fromCollectionsImportordereddict9D=dict ([("a", 1), ("b", 2), ("C", 3)])Ten Print(d) OneOd=ordereddict ([("a", 1), ("b", 2), ("C", 3)]) A Print(OD) -  -od1=ordereddict () theod1['Z']=1 -od1['y']=2 -od1['x']=3 - Print(Od1.keys ()) +  - #Ordereddict can implement a FIFO (first in, out) dict, deleting the first added key when the capacity exceeds the limit:
View CodeDefaultdict:
1 """2 when using Dict, if the referenced key does not exist, the keyerror is thrown. If you want the key to not exist, return a default value, you can use Defaultdict:3 """4  fromCollectionsImportdefaultdict5Dd=defaultdict (Lambda:"N /A")6dd['Key1']='ABC'7 Print(dd['Key2'])
View Code

Python Base Collections module

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.