Collections built-in modules of Python

Source: Internet
Author: User
Tags iterable
This article introduces the Collections built-in module of Python. collections is a built-in module of python. the Source Code is Lib/collections/init. py. This module provides a common data container.

Deque container object

Import from collections import deque. when creating a deque container object, you can set the parameter to Iterable object (such as tuple, list, str) or maxlen = x (int type) or None.

The deque container supports thread security. The time complexity is O (1) when elements are inserted or removed from both ends of deque through append or pop ). Compared with list objects, list objects share the same functions through the same APIs, but for list operations such as pop (0) or insert (0, x, the time complexity is O (n ).

If maxlen is not declared during deque initialization or maxlen = None, the deque container can accommodate any number of elements. Otherwise, the deque container is defined as an element container with a limited length.

Once the number of elements in the container reaches the configured maxlen, when a new element is added, the same number of elements will be excluded from the other end of the element, this ensures that all the elements in the current deque are newly added.

Deque object functions

Append (x)

Appendleft (x)

Clear ()

Copy ()

Count (x): returns the number of elements whose value is x in the container.

Extend (iterable)

Extendleft (iterable)

Index (x): The first element index with the value of x is found in the container. if the index does not exist, a ValueError error is thrown.

Insert (idx, x)

Pop ()

Popleft ()

Remove (x)

Reverse (): Flip the elements in the container and return None

Rotate (n)

Deque object read-only attribute

Maxlen

In addition to the preceding object functions, because the deque object is also an Iterable object, len (deque); reversed (deque); copy. copy (deque); copy. deepcopy (deque) and other functions also work. The in operator is also used to traverse the deque operation. the slicing operation deque [-1] can also return the last element in the container. If you operate on random elements in the container, we recommend that you use list.

Demo

Obtains the content of a line containing the python string in the file and the first three lines of the line.

from collections import dequedef search(lines, pattern, maxlen):    pre_lines = deque(maxlen=maxlen)    for line in lines:        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, end='')            print(line)

The content of the input text file is

c#cc++javascriptpythonjavadelphipythongolangperlcsshtmlpython

Output by code

cc++javascriptpythonpythonjavadelphipythonperlcsshtmlpython

The above is a detailed description of the Collections built-in module of Python. For more information, see other related articles in the first PHP community!

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.