Describes the built-in itertools module in Python.

Source: Internet
Author: User
Tags imap
This article mainly introduces the built-in itertools module in Python. the itertools module contains many common functions in Python and is a module that must be familiar with and mastered in learning Python, you can refer to the Python built-in module itertools to provide very useful functions for operating iterative objects.

First, let's take a look at several "infinite" iterators provided by itertools:

>>> import itertools>>> natuals = itertools.count(1)>>> for n in natuals:...   print n...123...

Because count () creates an infinite iterator, the above code will print out the natural number sequence and cannot stop at all. you can only press Ctrl + C to exit.

Cycle () repeats the input sequence infinitely:

>>> Import itertools >>> cs = itertools. cycle ('ABC') # note that a string is also a type of sequence >>> for c in cs :... print c... 'A 'B' 'C' a' A ''B 'B' 'C '...

It cannot be stopped.

Repeat () is used to infinitely repeat an element. However, if a second parameter is provided, the number of repetitions can be limited:

>>> ns = itertools.repeat('A', 10)>>> for n in ns:...   print n...

Print 10 times 'A'

An infinite sequence can be iterated infinitely only During for iteration. If an iteration object is created, it does not generate infinite elements in advance, in fact, it is impossible to create an infinite number of elements in the memory.

Although an infinite sequence can be iterated infinitely, we usually extract a finite sequence using functions such as takewhile () based on conditions:

>>> natuals = itertools.count(1)>>> ns = itertools.takewhile(lambda x: x <= 10, natuals)>>> for n in ns:...   print n...

Print 1 to 10

Itertools provides several iterator operation functions that are more useful:
Chain ()

Chain () can concatenate a group of iterative objects to form a larger iterator:

For c in chain ('ABC', 'XYZ'): print c # iteration result: 'A' B 'C' c' X' y' Z'

Groupby ()

Groupby () is used to pick out adjacent duplicate elements in the iterator and put them together:

>>> For key, group in itertools. groupby ('aaabbbccaa'):... print key, list (group) # Why is the list () function used here ?... A ['A', 'A', 'A'] B ['B', 'B', 'B'] C ['C ', 'C'] A ['A', 'A', 'A']

In fact, the selection rules are completed by functions. as long as the values returned by the two elements acting on the function are equal, these two elements are considered in a group, and the function return value is considered as the keys of the group. If we want to ignore case groups, we can make the elements 'A' and 'A' return the same key:

>>> for key, group in itertools.groupby('AaaBBbcCAAa', lambda c: c.upper()):...   print key, list(group)...A ['A', 'a', 'a']B ['B', 'B', 'b']C ['c', 'C']A ['A', 'A', 'a']

Imap ()

The difference between imap () and map () is that imap () can act on infinite sequences. if the two sequences have different lengths, the shorter one prevails.

>>> for x in itertools.imap(lambda x, y: x * y, [10, 20, 30], itertools.count(1)):...   print x...104090

Note that imap () returns an iteration object, while map () returns list. When you call map (), the calculation is completed:

>>> R = map (lambda x: x * x, [1, 2, 3]) >>> r # r has been computed [1, 4, 9]

When you call imap (), no computation is performed:

>>> R = itertools. imap (lambda x: x * x, [1, 2, 3]) >>> r
 
  
# R is just an iteration object
 

You must use the for loop to iterate on r to calculate the next element during each loop:

>>> for x in r:...   print x...149

This indicates that imap () implements "inert computing", that is, computing only when results are obtained. Functions similar to imap () can implement inert computing to process infinite sequences:

>>> r = itertools.imap(lambda x: x*x, itertools.count(1))>>> for n in itertools.takewhile(lambda x: x<100, r):...   print n...

What is the result?

What will happen if I change imap () to map () to process infinite sequences?

>>> r = map(lambda x: x*x, itertools.count(1))

What is the result?

Ifilter ()

Needless to say, ifilter () is the inert implementation of filter.
Summary

The itertools module provides all functions for processing iteration functions. their return values are not lists, but iteration objects. they are actually calculated only when the for loop iteration is used.

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.