Use of itertools library in Python standard library, pythonitertools

Source: Internet
Author: User

Use of itertools library in Python standard library, pythonitertools

Preface

Because there have not been many things recently, I want to write some technical articles to share with you, and also sort out the knowledge that I have accepted for a while, this is the truth.

Many people are committed to writing Python code more Pythonic, which is more compliant and easy to read. Second, Pythonic code is generally executed more efficiently. Today, I will introduce the Python system library itertools. I won't talk much about it below. Let's take a look at the detailed introduction.

Itertools Library

The iterator (generator) is a commonly used and useful data structure in Python. Compared with the list, the biggest advantage of the iterator is that it is used on demand for latency computing, this improves the development experience and running efficiency. In Python 3, operations such as map and filter do not return a list but an iterator.

Even so, the iterator we usually use is about range, and the list object is converted into an iterator object through the iter function, at this time, we should be playing the role itertools today.

Use itertools

Most of the functions in itertools return various iterator objects. Many of these functions can be achieved only by writing a lot of code at ordinary times, but are less efficient. After all, they are system libraries.

Itertools. accumulate

In short, it is accumulation.

>>> import itertools>>> x = itertools.accumulate(range(10))>>> print(list(x))[0, 1, 3, 6, 10, 15, 21, 28, 36, 45]

Itertools. chain

Connect multiple lists or iterators.

>>> x = itertools.chain(range(3), range(4), [3,2,1])>>> print(list(x))[0, 1, 2, 0, 1, 2, 3, 3, 2, 1]

Itertools. combinations

List or all combinations in which the specified number of elements in the generator are not repeated

>>> x = itertools.combinations(range(4), 3)>>> print(list(x))[(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)]

Itertools. combinations_with_replacement

Allow repeated element combinations

>>> x = itertools.combinations_with_replacement('ABC', 2)>>> print(list(x))[('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]

Itertools. compress

Filter elements by truth table

>>> x = itertools.compress(range(5), (True, False, True, True, False))>>> print(list(x))[0, 2, 3]

Itertools. count

It is a counter. You can specify the start position and step size.

>>> x = itertools.count(start=20, step=-1)>>> print(list(itertools.islice(x, 0, 10, 1)))[20, 19, 18, 17, 16, 15, 14, 13, 12, 11]

Itertools. cycle

List and iterator specified by Loop

>>> x = itertools.cycle('ABC')>>> print(list(itertools.islice(x, 0, 10, 1)))['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A']

Itertools. dropwhile

Discard the list and elements before the iterator according to the true value function

>>> x = itertools.dropwhile(lambda e: e < 5, range(10))>>> print(list(x))[5, 6, 7, 8, 9]

Itertools. filterfalse

Retain elements whose true values are False

>>> x = itertools.filterfalse(lambda e: e < 5, (1, 5, 3, 6, 9, 4))>>> print(list(x))[5, 6, 9]

Itertools. groupby

Groups elements based on the value of grouping functions.

>>> x = itertools.groupby(range(10), lambda x: x < 5 or x > 8)             >>> for condition, numbers in x:       ... print(condition, list(numbers))             True [0, 1, 2, 3, 4]        False [5, 6, 7, 8]        True [9] 

Itertools. islice

The previously used function slices the iterator.

>>> x = itertools.islice(range(10), 0, 9, 2)>>> print(list(x))[0, 2, 4, 6, 8] 

Itertools. permutations

Generates all sorts of elements of a specified number (sequence-related)

>>> x = itertools.permutations(range(4), 3)>>> print(list(x))[(0, 1, 2), (0, 1, 3), (0, 2, 1), (0, 2, 3), (0, 3, 1), (0, 3, 2), (1, 0, 2), (1, 0, 3), (1, 2, 0), (1, 2, 3), (1, 3, 0), (1, 3, 2), (2, 0, 1), (2, 0, 3), (2, 1, 0), (2, 1, 3), (2, 3, 0), (2, 3, 1), (3, 0, 1), (3, 0, 2), (3, 1, 0), (3, 1, 2), (3, 2, 0), (3, 2, 1)] 

Itertools. product

(Product) that generates multiple lists and iterators)

>>> x = itertools.product('ABC', range(3))>>>>>> print(list(x))[('A', 0), ('A', 1), ('A', 2), ('B', 0), ('B', 1), ('B', 2), ('C', 0), ('C', 1), ('C', 2)]

Itertools. repeat

Generate an iterator with a specified number of elements

>>> x = itertools.repeat(0, 5)>>> print(list(x))[0, 0, 0, 0, 0] 

Itertools. starmap

Similar to map

>>> x = itertools.starmap(str.islower, 'aBCDefGhI')>>> print(list(x))[True, False, False, False, True, True, False, True, False]

Itertools. takewhile

In contrast to dropwhile, elements are retained until the true value of the function is false.

>>> x = itertools.takewhile(lambda e: e < 5, range(10))>>> print(list(x))[0, 1, 2, 3, 4]

Itertools. tee

I am not very familiar with this function. It seems that it is to generate a specified number of iterators.

>>> x = itertools.tee(range(10), 2)>>> for letters in x:... print(list(letters))...[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

Itertools.zip _ longest

Similar to zip, but the long list and iterator length are subject

>>> x = itertools.zip_longest(range(3), range(5))>>> y = zip(range(3), range(5))>>> print(list(x))[(0, 0), (1, 1), (2, 2), (None, 3), (None, 4)]>>> print(list(y))[(0, 0), (1, 1), (2, 2)] 

Conclusion

I would like to summarize it here, but to be honest, the features and libraries of Python must be used in a variety of ways to be proficient. In the end, it can be easily achieved.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.