Usage of itertools module in Python

Source: Internet
Author: User
This article mainly introduces the use of the itertools module in Python, describes the usage of common functions in the itertools module in detail, and provides examples to help you gain a deep understanding of Python program design, for more information about itertools in Python, see the following example. The specific analysis is as follows:

Generally, the itertools module contains functions used to create an effective iterator. you can use various methods to perform cyclic operations on data, all functions in this module can return iterators that can be used together with for loop statements and other functions that contain iterators (such as generator and generator expressions.

Chain (iter1, iter2,..., iterN ):

A group of iterators (iter1, iter2 ,..., iterN), this function creates a new iterator to link all the iterators. the returned iterator starts generating items from iter1, knows that iter1 is used up, and then generates items from iter2, this process continues until all items in iterN are used up.

from itertools import chaintest = chain('AB', 'CDE', 'F')for el in test:  print elABCDEF

Chain. from_iterable (iterables ):

An alternative chain constructor where iterables is an iteration variable that generates an iteration sequence. the result of this operation is the same as that generated by the following generator code snippet:

>>> def f(iterables):  for x in iterables:    for y in x:      yield y>>> test = f('ABCDEF')>>> test.next()'A'>>> from itertools import chain>>> test = chain.from_iterable('ABCDEF')>>> test.next()'A'

Combinations (iterable, r ):

Create an iterator and return all the sub-sequences whose lengths are r in iterable. the items in the returned sub-sequences are sorted by the order in the input iterable:

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

Count ([n]):

Create an iterator to generate continuous integers starting from n. If n is ignored, the iterator starts from 0 (note: long integers are not supported). If sys is exceeded. maxint, the counter will overflow and continue from-sys. the maxint-1 starts to calculate.

Cycle (iterable ):

Create an iterator to repeatedly perform loop operations on the elements in the iterable. a copy of the elements in the iterable is generated internally. This copy is used to return repeated items in the loop.

Dropwhile (predicate, iterable ):

Create an iterator. if the predicate (item) function is True, items in iterable are discarded. If predicate returns False, items in iterable and all subsequent items are generated.

def dropwhile(predicate, iterable):  # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1  iterable = iter(iterable)  for x in iterable:    if not predicate(x):      yield x      break  for x in iterable:    yield x

Groupby (iterable [, key]):

Create an iterator to group consecutive items generated by iterable. duplicate items are searched during grouping.

If iterable generates the same item in multiple consecutive iterations, a group is defined. if this function is applied to a category list, the group defines all unique items in the list, key (if provided) is a function applied to each item. if this function has a return value, the value will be used for subsequent items instead of the item itself for comparison, the iterator generation element (key, group) returned by this function, where key is the key value of the group, and group is the iterator that generates all the items that constitute the group.

Ifilter (predicate, iterable ):
Create an iterator and generate only the items whose predicate (item) is True in iterable. If predicate is None, all items in iterable that are calculated as True will be returned.

ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9


Ifilterfalse (predicate, iterable ):
Create an iterator to generate only the items whose predicate (item) is False in iterable. If predicate is None, all items in iterable that are calculated as False are returned.

ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8

Imap (function, iter1, iter2, iter3,..., iterN)
Create an iterator to generate the function (i1, i2 ,..., iN), where i1, i2... iN comes from iter1, iter2... iterN. if function is None, return (i1, i2 ,..., iN), as long as the provided iterator does not generate a value, the iteration stops.

>>> from itertools import * >>> d = imap(pow, (2,3,10), (5,2,3)) >>> for i in d: print i  32 9 1000  #### >>> d = imap(pow, (2,3,10), (5,2)) >>> for i in d: print i  32 9 #### >>> d = imap(None, (2,3,10), (5,2)) >>> for i in d : print i  (2, 5) (3, 2)

Islice (iterable, [start,] stop [, step]):
Create an iterator to generate items in a way similar to the slice return value: iterable [start: stop: step]. the previous start items are skipped and the iteration stops at the position specified by stop, step specifies the stride used to skip an item. Unlike slices, negative values are not used for start, stop, or step. if start is omitted, the iteration starts from 0. If step is omitted, the stride is 1.

def islice(iterable, *args):   # islice('ABCDEFG', 2) --> A B   # islice('ABCDEFG', 2, 4) --> C D   # islice('ABCDEFG', 2, None) --> C D E F G   # islice('ABCDEFG', 0, None, 2) --> A C E G   s = slice(*args)   it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1))   nexti = next(it)   for i, element in enumerate(iterable):     if i == nexti:       yield element       nexti = next(it) #If start is None, then iteration starts at zero. If step is None, then the step defaults to one.#Changed in version 2.5: accept None values for default start and step.

Izip (iter1, iter2,... iterN ):
Create an iterator to generate tuples (i1, i2 ,... iN), where i1, i2... iN comes from iter1, iter2... iterN: the iteration stops when a provided iterator no longer generates a value. The generated value of this function is the same as that of the built-in zip () function.

def izip(*iterables):   # izip('ABCD', 'xy') --> Ax By   iterables = map(iter, iterables)   while iterables:     yield tuple(map(next, iterables))

Izip_longest (iter1, iter2,... iterN, [fillvalue = None]):
It is the same as izip (), but the iteration process continues until all input iteration variables iter1 and iter2 are exhausted. if the fillvalue keyword parameter is not used to specify different values, use None to fill in the value of the used iteration variable.

def izip_longest(*args, **kwds):   # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-   fillvalue = kwds.get('fillvalue')   def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):     yield counter()     # yields the fillvalue, or raises IndexError   fillers = repeat(fillvalue)   iters = [chain(it, sentinel(), fillers) for it in args]   try:     for tup in izip(*iters):       yield tup   except IndexError:     pass

Permutations (iterable [, r]):

Create an iterator and return all the project sequences whose lengths are r in iterable. if r is omitted, the sequence length is the same as the number of projects in iterable:

def permutations(iterable, r=None):   # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC   # permutations(range(3)) --> 012 021 102 120 201 210   pool = tuple(iterable)   n = len(pool)   r = n if r is None else r   if r > n:     return   indices = range(n)   cycles = range(n, n-r, -1)   yield tuple(pool[i] for i in indices[:r])   while n:     for i in reversed(range(r)):       cycles[i] -= 1       if cycles[i] == 0:         indices[i:] = indices[i+1:] + indices[i:i+1]         cycles[i] = n - i       else:         j = cycles[i]         indices[i], indices[-j] = indices[-j], indices[i]         yield tuple(pool[i] for i in indices[:r])         break     else:       return

Product (iter1, iter2,... iterN, [repeat = 1]):

Create an iterator that generates tuples that represent the Cartesian product of projects such as item1 and item2. repeat is a keyword parameter that specifies the number of times a sequence is generated repeatedly.

def product(*args, **kwds):   # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy   # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111   pools = map(tuple, args) * kwds.get('repeat', 1)   result = [[]]   for pool in pools:     result = [x+[y] for x in result for y in pool]   for prod in result:     yield tuple(prod)

Repeat (object [, times]):
Create an iterator to repeatedly generate an object. times (if already provided) specifies the repetition count. if times is not provided, all times are returned.

def repeat(object, times=None):   # repeat(10, 3) --> 10 10 10   if times is None:     while True:       yield object   else:     for i in xrange(times):       yield object

Starmap (func [, iterable]):
Create an iterator and generate the value func (* item). item comes from iterable. This function is valid only when iterable generates an item that applies to this method of calling a function.

def starmap(function, iterable):   # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000   for args in iterable:     yield function(*args)

Takewhile (predicate [, iterable]):
Create an iterator to generate the items whose predicate (item) is True in iterable. if the value of predicate is calculated as False, the iteration stops immediately.

def takewhile(predicate, iterable):   # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4   for x in iterable:     if predicate(x):       yield x     else:       break

Tee (iterable [, n]):
Create n independent iterators from iterable. The new iterator is returned in the form of n tuples. the default value of n is 2. this function applies to any iteratable objects. However, to clone the original iterator, the generated items are cached and used in all newly created iterators. Be sure not to call tee () then use the original iterator iterable. Otherwise, the cache mechanism may not work correctly.

def tee(iterable, n=2):  it = iter(iterable)  deques = [collections.deque() for i in range(n)]  def gen(mydeque):    while True:      if not mydeque:       # when the local deque is empty        newval = next(it)    # fetch a new value and        for d in deques:    # load it to all the deques          d.append(newval)      yield mydeque.popleft()  return tuple(gen(d) for d in deques)#Once tee() has made a split, the original iterable should not be used anywhere else; otherwise, the iterable could get advanced without the tee objects being informed.#This itertool may require significant auxiliary storage (depending on how much temporary data needs to be stored). In general, if one iterator uses most or all of the data before another iterator starts, it is faster to use list() instead of tee().

I believe this article has some reference value for everyone's learning about Python programming.

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.