Python daily Class (5): Itertools module

Source: Internet
Author: User
Tags imap iterable pow

The Itertools module contains functions to create a valid iterator that can be used in various ways to iterate over the data, and the iterators returned by all functions in this module can be combined with a For loop statement and other functions that contain iterators, such as generators and generator expressions.

Chain (Iter1, Iter2, ..., itern):

Given a set of iterators (Iter1, Iter2, ..., itern), this function creates a new iterator to link all iterators, the returned iterator builds the item from Iter1, knows that Iter1 is exhausted, and then generates the item from Iter2, This process continues until all items in the Itern are exhausted.

1 from Itertools import chain 2 test = Chain (' AB ', ' CDE ', ' F ') 3 for El in Test:4     print el 5  6 A 7 B 8 C 9 D10 E1 0 '

Here's an example of Fibonacci:

 fromItertoolsImportchainclassFei: @staticmethoddefFibon (N): a=b=1 forIinchrange (n):yieldA A, b=b,a+bdef __iter__(self):returnFei.fibon (100) Fei=Fei () test=chain (, Fei) forElinchTest:Print(EL)

  

Chain.from_iterable (iterables):

An alternate chain constructor in which the Iterables is an iterative variable that generates an iterative sequence that results in the same result as the following generator code fragment:

1 >>> def f (iterables): 2 for     x in Iterables:3 for y in         x:4             yield y 5  6 >>> test = f (' ABCDEF ') 7 >>> test.next () 8 ' A ' 9 >>> from itertools import chain12 >>> test = Chain.fro M_iterable (' ABCDEF ') >>> test.next () ' A '

Here's a description:

Combinations (iterable, R):

Creates an iterator that returns all iterable of length r in the column, and the items in the returned subsequence are sorted in the order of the input iterable:

>>> from itertools import combinations>>> test = combinations ([1,2,3,4], 2) >>> for El in test:< C0/>print El    (1, 2) (1, 3) (1, 4) (2, 3) (2, 4) (3, 4)

  

Count ([n]):

Creates an iterator that generates a sequential integer starting with n, and if n is omitted, the calculation starts at 0 (note: This iterator does not support long integers), and if Sys.maxint is exceeded, the counter overflows and continues to be calculated from-sys.maxint-1.

Cycle (iterable):

Creates an iterator that iterates over the elements in the iterable, and internally generates a copy of the elements in the iterable, which is used to return duplicates in the loop.

Dropwhile (predicate, iterable):

Creates an iterator that discards an item in Iterable if the function predicate (item) is true, and if predicate returns FALSE, the item in iterable and all subsequent items are generated.

def dropwhile (predicate, iterable): 2     # dropwhile (Lambda x:x<5, [1,4,6,4,1])-6 4     iterable = ITER (iter ABLE) 4 for     x in Iterable:5         if not predicate (x): 6             yield x7             break8 for x in     iterable:9         yield x

  

GroupBy (iterable [, key]):

Creates an iterator that groups successive items generated by iterable and finds duplicates during grouping.

If iterable generates the same item in multiple successive iterations, a group is defined, and if the function is applied to a categorical list, then the grouping defines all the unique items in the list, and key (if provided) is a function that applies to each item if the function has a return value. The value will be used for subsequent entries instead of the item itself for comparison, and this function returns an iterator generating element (key, group), where key is a grouped key value, and group is an iterator that generates all the items that make up the group.

IFilter (predicate, iterable):

Creates an iterator that generates only the item predicate (item) is true in iterable, and if predicate is none, returns all items in iterable that are evaluated as true.

IFilter (Lambda x:x%2, Range)--1 3 5 7 9

Ifilterfalse (predicate, iterable):

Creates an iterator that only generates an item with predicate (item) in Iterable, and if predicate is none, returns all items in iterable that are evaluated as false.

Ifilterfalse (Lambda x:x%2, Range)--0 2 4 6 8

IMAP (function, Iter1, Iter2, Iter3, ..., Itern)

Create an iterator that generates the item function (I1, I2, ..., in), where i1,i2...in is from the iterator iter1,iter2 ... itern, if function is None, return (I1, I2, ..., in) form of a tuple, the iteration stops as long as the supplied iterator no longer generates a value.

>>> from itertools Import * 2   >>> d = IMAP (POW, (2,3,10), (5,2,3)) 3   >>> for I in D:PR int i 4    5   6   9 7 +   8    9  # # #10  >>> d = IMAP (POW, (2,3,10), (5,2))  > >> for I in D:print i12  3214  915  # # # #17  >>> d = IMAP (None, (2,3,10), (5,2))  >>> for I in D:print i19  (2, 5)  (3, 2)

Http://www.cnblogs.com/cython/articles/2169009.html

Python daily Class (5): Itertools module

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.