Python Collections Module

Source: Internet
Author: User
Tags types of extensions

There are some of the more useful types in the collections module, which are some of the basic types of extensions that are necessary for one of the modules


    1. Counter (counter)

Can track the number of occurrences of a value, and is arranged from large to small

>>> Import collections>>> C1 = collections. Counter (' ADADWEFFFCVCC ') >>> c1counter ({' C ': 3, ' F ': 3, ' a ': 2, ' d ': 2, ' E ': 1, ' W ': 1, ' V ': 1})





2.OrderedDict (ordered dictionary)

The dictionary types in Python are unordered, sometimes disorderly, and can be ordereddict to generate an ordered dictionary in the order in which they are inserted.

>>> import collections>>> D = dict ([' K1 ', one-to-one), (' K2 ', ' + '), (' K3 ', ' + ')]) >>> d{' K3 ':, ' K2 ': 22, ' K1 ': 11}>>> order_d = collections. Ordereddict ([' K1 ', one-to-one), (' K2 ', ') ', (' K3 ', ')]) >>> order_dordereddict ([' K1 ', one-by-one), (' The [') ', ' + '), (' K2 ', 33)])




3.defaultdict (Default dictionary)

Set a default type for the value in the dictionary, which can be list, tuple, etc.

>>> import collections>>> dic = collections.defaultdict (list) >>> dic[' K1 '].append (1) > >> dicdefaultdict (<type ' list ', {' K1 ': [1]}) >>> dic = {}>>> dic[' k1 '] = []>>> dic [' K1 '].append (1) >>> dic{' K1 ': [1]}




4.namedtuple (can be named tuples)

This is not the same as the normal tuple creation process, the general tuple is to create the object with a class that already exists in Python, then use the object, then create the class, create the object through this class, and then use the object. It is a subclass of a tuple in python that inherits the methods of tuples in Python.

>>> import collections>>> mytuple = collections.namedtuple (' mytuple ', [' X ', ' y ']) >>> new = Mytuple >>> Print newmytuple (x=1, y=2) >>> new.x1>>> new.y2





5.deque (bidirectional queue)

Both can be taken at both ends, can be inserted, greatly improve the efficiency of insertion and removal

>>> import collections>>> D = Collections.deque ([11,22,55,66]) >>> ddeque ([11, 22, 55, 66])


This article from "Chu Water June" blog, please be sure to keep this source http://artvary.blog.51cto.com/10506823/1894589

Python Collections Module

Related Article

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.