The itertools of the Python standard library

Source: Internet
Author: User

Itertools Library

Iterators (generators) are a very common and useful data structure in Python, and the biggest advantage of iterators over lists (list) is that they are deferred and used on demand.

Several methods of Itertools library are introduced:

itertools.accumulate: List Accumulation method

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

Itertools.chain: How to synthesize multiple lists into a list

>>> x = [1,2,3]>>> y = [4,5,6]>>> a = Itertools.chain (x, y) >>> print (list (a)) [1, 2, 3, 4, 5, 6]

itertools.combinations: All combinations of the specified number of elements in the list are not duplicated

Note: 2 means to find a random set of 2 elements in a list with no duplicates

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

Itertools.islice: Slicing a list

Note: 0 indicates the starting position, 5 means the end position, and 2 indicates the step

>>> x = [1,2,3,4,5]>>> a = Itertools.islice (x,0,5,2) >>> print (list (a)) [1, 3, 5]

itertools.count: Counter, can specify start position and step size

Note: Start indicates the starting position, step is the step

>>> x = Itertools.count (start=2,step=3) >>> print (List (Itertools.islice (x,5))) [2, 5, 8, 11, 14]

itertools.cycle: Data in a loop list or iterator

>>> x = itertools.cycle (' ABCD ') >>> print (List (Itertools.islice (x,0,10,2))) [' A ', ' C ', ' A ', ' C ', ' a ']

itertools.repeat: Generates an iterator with the specified number of elements and the same element

>>> Print (List (itertools.repeat (' Liuwei ', 5))) [' Liuwei ', ' Liuwei ', ' Liuwei ', ' Liuwei ', ' Liuwei ']# You can also use the following method to generate a list of the same elements >>> item = ["Liu"]*5>>> print (item) [' Liu ', ' Liu ', ' Liu ', ' Liu ', ' Liu ']

There's a lot more to be done.

This article is from the "burning Years of Passion" blog, please be sure to keep this source http://liuzhengwei521.blog.51cto.com/4855442/1919807

The itertools of the Python standard library

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.