Three itertools of common Python modules

Source: Internet
Author: User
Tags imap iterable

The Itertools module, introduced after python2.6, contains functions that create effective iterators, which can be used in various ways to iterate over the data.

The iterator returned by all functions in this module can be used in conjunction with a For Loop statement and other functions that contain iterators, such as generators and generator expressions.

Note the functions in the Itertools module are created with objects and are iterative objects.

1, Itertools.count (start=0, Step=1)

Creates an iterator that generates a sequential integer starting at N, and computes from 0 if n is omitted (note: This iterator does not support long integers)

If the sys.maxint is exceeded, the counter overflows and continues to be calculated from-sys.maxint-1

From Itertools import *for i in Izip (count (1), [' A ', ' B ', ' C ']):    Print I (1, ' a ') (2, ' B ') (3, ' C ')

2, Itertools.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.

From itertools Import *i = 0for item in cycle ([' A ', ' B ', ' C ']):    i + = 1    if i = = Ten: Break    Print (I, item) (1 , ' A ') (2, ' B ') (3, ' C ') (4, ' A ') (5, ' B ') (6, ' C ') (7, ' a ') (8, ' B ') (9, ' C ')

3, Itertools.repeat (object[, Times])

Creates an iterator that repeats the build object,times (if provided) to specify a repeating count, and returns the object without a times.

From Itertools import *for i in repeat (' Over-and-over ', 5):    Print Iover-and-overover-and-overover-and-overover-and-overover-and-over

4, Itertools.imap (function, *iterables)

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.

That is: Returns an iterator that invokes a function whose value is on the input iterator and returns the result. It is similar to the built-in function map (), except that it stops at the end of any input iterator (instead of inserting the none value to complement all of the inputs).

An iterator that returns a sequence of values that each element of the sequence returns after being executed by Func, much like the map () function.

From itertools import *print ' doubles: ' For I in IMAP (Lambda X:2*x, xrange (5)):    print iprint ' multiples: ' For I in IMAP (  Lambda x, Y: (x, Y, x*y), Xrange (5), xrange (5,10)):    print '%d *%d =%d '% idoubles:02468multiples:0 * 5 = 01 * 6 = 62 * 7 = 143 * 8 = 244 * 9 = 36

5, Itertools.chain (*iterables)

Multiple iterators are used as parameters, but only a single iterator is returned, which produces the contents of all the parameter iterators as if they were from a single sequence.

From Itertools import *for I in Chain ([1, 2, 3], [' A ', ' B ', ' C ']):    print i123abcfrom itertools import chain, Imapdef F Latmap (f, items):    return chain.from_iterable (IMAP (f, items)) >>> list (Flatmap (Os.listdir, dirs)) > >> [' settings.py ', ' wsgi.py ', ' templates ', ' app.py ',     ' templates ', ' index.html, ' Config.json ']

6, Itertools.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.

That is: The first time after the condition is false, returns the remaining items in the iterator.

From Itertools import *def should_drop (x):    print ' testing: ', x    return (x<1) for I in Dropwhile (Should_drop, [1 , 0, 1, 2, 3, 4, 1,-2]):    print ' yielding: ', itesting: -1testing:0testing:1yielding:1yielding:2yielding:3yieldin G:4yielding:1yielding:-2

7, Itertools.groupby (iterable[, key])

Returns an iterator that produces a collection of values grouped by key.

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.

That is, the results of each element of the sequence are grouped according to the Keyfunc function (each grouping is an iterator), and these grouped iterators are returned.

From itertools Import Groupbyqs = [{' Date ': 1},{' date ': 2}][(name, List (group)) for name, group in Itertools.groupby (QS,  Lambda p:p[' date ')]out[77]: [(1, [{' Date ': 1}]), (2, [{' Date ': 2}])]>>> from itertools import *>>> a = [' AA ', ' AB ', ' abc ', ' BCD ', ' ABCDE ']>>> for I, K in GroupBy (A, Len): ...     Print I, list (k) ... 2 [' AA ', ' ab ']3 [' abc ', ' BCD ']5 [' ABCDE ']

8, Itertools.ifilter (predicate, iterable)

The return iterator is similar to the built-in function filter () for the list, which only includes the item when the test function returns TRUE. It differs from Dropwhile ()

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

Performs an iterator to the function func that returns the true element.

From Itertools import *def check_item (x):    print ' testing: ', x    return (x<1) for I in IFilter (Check_item, [-1, 0, 1, 2, 3, 4, 1,-2]):    print ' yielding: ', itesting: -1yielding: -1testing:0yielding:0testing:1testing:2testing:3t Esting:4testing:1testing: -2yielding:-2

9, Itertools.ifilterfalse (predicate, iterable)

In contrast to IFilter (the function returns an iterator that contains the items whose test function returns false)

Creates an iterator that only generates an entry with predicate (item) in Iterable, and if predicate is none, returns an iterator to the function func that returns false elements for all items evaluated as false in Iterable.

From Itertools import *def check_item (x):    print ' testing: ', x    return (x<1) for I in Ifilterfalse (Check_item, [- 1, 0, 1, 2, 3, 4, 1,-2]):    print ' yielding: ', itesting: -1testing:0testing:1yielding:1testing:2yielding:2testing : 3yielding:3testing:4yielding:4testing:1yielding:1testing:-2

10, Itertools.islice (iterable, stop)

Itertools.islice (iterable, Start, stop[, step])

The returned iterator is an entry that returns an input iterator based on the index selected

Creates an iterator that generates an item in a way similar to the slice return value: iterable[start:stop:step], skips the previous start item, iterates over the stop at the specified stop, and step specifies the stride used to skip the item. Unlike slices, negative values are not used for any start,stop and step, and if Start is omitted, the iteration will start at 0 and if step is omitted, the stride will take 1.

Returns the iterator of the sequence seq from start to end of stop step for the element of step.

From itertools import *print ' Stop @ 5: ' For I in Islice (count (), 5):    print IPrint ' Start at 5, Stop at: ' For I Slice (count (), 5,):    print IPrint ' by tens to: ' For I in Islice (count (), 0, ten):    print istop at 5:01234s Tart at 5, Stop at 10:56789by tens to 100:0102030405060708090

11, Itertools.starmap (function, iterable)

Creates an iterator that generates the value func (*item), where item is from iterable, which is valid only if the Iterable-generated item applies to the way this function is called.

Executes on each element of the sequence seq as a parameter list of Func, returning an iterator that executes the result.

From itertools Import *values = [(0, 5), (1, 6), (2, 7), (3, 8), (4, 9)]for I in Starmap (lambda x, Y: (x, Y, x*y), values): C0/>print '%d *%d =%d '% I0 * 5 = 01 * 6 = 62 * 7 = 143 * 8 = 244 * 9 = 36

12, Itertools.tee (iterable[, n=2])

Returns some independent iterators based on a single raw input (default is 2). It is somewhat semantically similar to the Tee Tools on Unix, meaning that they read the values in the input device repeatedly and write the values to a named file and standard output

Create n independent iterators from Iterable, the created iterator is returned as an n-tuple, the default value of N is 2, and this function applies to any object that can be iterated, but in order to clone the original iterator, the resulting item is cached and used in all newly created iterators, be sure not to call the tee () then use the original iterator iterable, otherwise the caching mechanism may not work correctly.

Divides an iterator into n iterators and returns a tuple. The default is two

From itertools Import *r = Islice (count (), 5) i1, i2 = Tee (R) for I in I1:    print ' I1: ', IFOR I in I2:    print ' i2: ', I I1:0i1:1i1:2i1:3i1:4i2:0i2:1i2:2i2:3i2:4

13, Itertools.takewhile (predicate, iterable)

Contrary to Dropwhile,

Creates an iterator that generates an item with a true predicate (item) in Iterable, and as long as the predicate evaluates to False, the iteration stops immediately.

That is, start from the head of the sequence until the Execute function func fails.

From Itertools import *def should_take (x):    print ' testing: ', x    return (X<2) for I in TakeWhile (Should_take, [1 , 0, 1, 2, 3, 4, 1,-2]):    print ' yielding: ', itesting: -1yielding: -1testing:0yielding:0testing:1yielding:1testin G:2

14, Itertools.izip (*iterables)

Returns an iterator that incorporates multiple iterators as a tuple. It is similar to the built-in function zip (), except that it returns an iterator instead of a list

Create an iterator that generates a tuple (I1, I2, ... in), where i1,i2 ... in is from the iterator iter1,iter2 ... itern, as long as one of the provided iterators no longer generates a value, the iteration stops, and the function generates the same value as the built-in zip () function.

From Itertools import *for i in Izip ([1, 2, 3], [' A ', ' B ', ' C ']):    Print I (1, ' a ') (2, ' B ') (3, ' C ')

15, Itertools.izip_longest (*iterables[, Fillvalue])

Same as Izip (), but the iterative process continues until all input iteration variables iter1,iter2 are exhausted, and none is used to populate the values of the iteration variables that are already in use if no different values are specified using the Fillvalue keyword parameter.

Class zipexhausted (Exception):    passdef izip_longest (*args, **kwds):    # izip_longest (' ABCD ', ' xy ', fillvalue= ' --')--Ax by c-d-    fillvalue = kwds.get (' fillvalue ')    counter = [Len (args)-1]    def Sentinel ():        if not COUNTER[0]:            raise zipexhausted        counter[0]-= 1        yield fillvalue    fillers = repeat (fillvalue)    iterators = [Chain (it, Sentinel (), fillers) for it in args]    try: While        iterators:            yield tuple (map (Next, it erators))    except zipexhausted:        Pass

For more information please refer to http://wklken.me/posts/2013/08/20/python-extra-itertools.html (here lazy paste copied)

Three itertools of common Python modules

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.