Python's collections built-in module detailed description

Source: Internet
Author: User
Tags iterable
The collections is a python built-in module with source code located in lib/collections/init.py, which provides a common data container.

Deque Container Object

Introduced by the From collections import deque, when you create a Deque container object, you can initialize the Iterable object (such as TUPLE,LIST,STR) or maxlen=x (int type) or None by setting parameters.

The Deque container supports thread safety, and the time complexity is O (1) when inserting or removing elements through append or pop on both ends of the deque. The list also has the same API for the same functionality as the list object, but the time complexity is O (n) for the operation of the list, such as pop (0) or insert (0, X).

If you do not declare maxlen or declare maxlen=none when initializing deque, the Deque container can hold any number of elements, otherwise the Deque container is defined as a finite-length element container.

Once the number of elements in the container reaches the set MaxLen, when a new element is added, the same number of elements is excluded at the other end of the join element, which guarantees that the elements in the current deque are all newly added elements.

Deque Object Functions

Append (x)

Appendleft (x)

Clear ()

Copy ()

Count (x): Returns the number of elements in the container that have a value of X

Extend (iterable)

Extendleft (iterable)

Index (x): The first element in the container that has a value of x is indexed, and if it does not, throws a ValueError exception

Insert (idx, x)

Pop ()

Popleft ()

Remove (x)

Reverse (): Flips the element in the container and returns None

Rotate (N)

Deque object Read-only property

MaxLen

In addition to the object functions described above, Len (deque), reversed (deque) copy.copy (deque), Copy.deepcopy (deque), and so on, because the Deque object is also a Iterable object, also The in operator is also used when traversing the deque operation, and the slice operation Deque[-1] can also return the last element in the container. It is recommended to use list if you are manipulating random elements in a container.

Demo

Gets the line of the Python string in the file, and the first three lines of this line of content

From collections import Dequedef search (lines, pattern, maxlen):    pre_lines = deque (Maxlen=maxlen) for line in    Lin ES:        If pattern in line:            yield line, pre_lines        pre_lines.append (line) if name = = ' main ': With    (open ('./ Test.txt ') as F:        for line, pre_lines in search (f, ' Python ', 3): For            pre_line in Pre_lines:                print (Pre_line, en D= ")            print (line)

Enter the contents of the text file as

C#cc++javascriptpythonjavadelphipythongolangperlcsshtmlpython

Output by Code as

Cc++javascriptpythonpythonjavadelphipythonperlcsshtmlpython
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.