Python standard library loop (Itertools)

Source: Internet
Author: User
Tags imap pow in python

In the Loop object and the function object, we understand the function of the loop (iterator). A loop is a container for an object that contains multiple objects. By invoking the next () method of the Loop (__next__ () method, in Python 3.x), the loop returns an object in turn. Until all objects are traversed, the loop will cite stopiteration errors.

In the for-I in iterator structure, each returned object of the loop is given to I until the loop ends. Using the ITER () built-in function, we can change containers such as tables and dictionaries into loops. Like what

For I in ITER ([2, 4, 5, 6]):
    print (i)

The Itertools package in the standard library provides a more flexible tool for generating loops. The input of these tools is mostly the existing loop device. On the other hand, these tools are completely free to use Python implementations, which simply provide a more standard and efficient way to implement them. This also conforms to the Python "only and best solution only" concept.

# import the tools from
itertools Import *

Infinite loop device

Count (5, 2) #从5开始的整数循环器, increase each time by 2, that is 5, 7, 9, 11, 13, 15 ...

Cycle (' abc ') #重复序列的元素, both A, B, C, a, B, c ...

Repeat (1.2) #重复1.2, forming an infinite loop, i.e. 1.2, 1.2, 1.2, ...

Repeat can also have a limit of times:

Repeat (5) #重复10, repeated 5 times

Functional Tools

Functional programming is the programming paradigm of the function itself as a processing object. In Python, functions are also objects, so you can easily perform some functional processing, such as map (), filter (), and reduce () functions.

The itertools contains similar tools. These functions receive functions as arguments and return the result as a loop.

Like what

From itertools import *
    
RLT = IMAP (POW, [1, 2, 3], [1, 2, 3]) for
    
num in RLT:
    print (num)

The IMAP function is shown above. This function is similar to the map () function, except that it returns a loop instead of a sequence. Contains elements 1, 4, 27, i.e. 1**1, 2**2, 3**3 results. Function pow (the built-in exponentiation function) as the first argument. Pow (), in turn, acts on each element of the following two lists and collects the function results, which form the returned loop.

In addition, you can use the following function:

Starmap (POW, [(1, 1), (2, 2), (3, 3)])

POW will act on each tuple of the table in turn.

The IFilter function is similar to the filter () function, except that it returns a loop.

IFilter (lambda x:x > 5, [2, 3, 5, 6, 7]

The lambda function is applied to each element in turn, and if the function returns True, the original element is collected. 6, 7

Furthermore

Ifilterfalse (lambda x:x > 5, [2, 3, 5, 6, 7])

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.