Original: http://www.cnblogs.com/vamei/p/3174796.html

Vamei

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

In the for- i in iterator structure, each time the loop returns an object, it is assigned to I until the loop ends. Using the iter () built-in function, we can turn containers such as tables, dictionaries, and so on into circulators. Like what

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

The itertools package in the standard library provides a more flexible tool for generating the circulator. Most of the input to these tools is the existing circulator. On the other hand, these tools are completely self-fulfilling with Python, which simply provides a relatively standard and efficient way to implement them. This is also in line with Python's "only and best solution only" concept.

# Import the Tools from Import *

Infinite loop Device

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

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

repeat (1.2) #重复1.2, constituting an infinite circulator, i.e. 1.2, 1.2, 1.2, ...

Repeat can also have a frequency limit:

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 do some functional processing, such as map (), filter (), and reduce () functions.

The itertools contains similar tools. These functions receive functions as parameters and return the result as a circulator.

Like what

from Import *= IMAP (POW, [1, 2, 3], [1, 2, 3]) for 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 The result of element 1, 4, 27, which is 1**1, 2**2, 3**3. Function Pow (built-in exponentiation function) as the first parameter. The POW () acts on each element of the next two lists in turn, and collects the result of the function, forming the returned circulator.

In addition, you can also use the following function:

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

The 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 circulator.

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

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

In addition

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

Similar to the above, but collects elements that return false. 2, 3, 5

TakeWhile (Lambda x:x < 5, [1, 3, 6, 7, 1])

When the function returns True, the element is collected to the circulator. Once the function returns false, it stops. 1, 3

Dropwhile (Lambda x:x < 5, [1, 3, 6, 7, 1])

When the function returns False, the element is skipped. Once the function returns True, it starts collecting all the remaining elements into the circulator. 6, 7, 1

Combo Tool

We can get a new circulator by combining the original circulator.

chain ([1, 2, 3], [4, 5, 7]) # Connect two circulators to become one. 1, 2, 3, 4, 5, 7

Product (' abc ', [1, 2]) # The Cartesian product of multiple circulator sets. Equivalent to nested loops

for in product ('abc', [1, 2]): print m, n

Permutations (' abc ', 2) # Pick two elements from ' ABCD ', such as AB, BC, ... Sorts all the results back to the new circulator.

Note that the above combinations are sorted in order, i.e. AB, BA are returned.

Combinations (' abc ', 2) # Pick two elements from ' ABCD ', such as AB, BC, ... Sorts all the results back to the new circulator.

Note that the above combinations are not sorted in order, i.e. AB, ba words, only return an AB.

combinations_with_replacement (' abc ', 2) # is similar to above but allows two selected elements to repeat. That's a lot more AA, BB, CC

GroupBy ()

The key function is applied to each element of the original circulator. Based on the result of the key function, the elements that have the same function result are divided into a new circulator. Each new circulator returns the result as a label for the function.

It's like a group of people's height as a circulator. We can use such a key function: If the height is greater than 180, return "tall", if the height is 160, return "short", the Middle return "middle". Eventually, all the height will be divided into three circulator, namely "tall", "short", "Middle".

defHeight_class (h):ifH > 180: return "Tall" elifH < 160: return " Short" Else: return "Middle"Friends= [191, 158, 159, 165, 170, 177, 181, 182, 190]friends= sorted (Friends, key =Height_class) forM, ninchGroupBy (friends, key =height_class):Print(m)

Print(list (n))

Note that the GroupBy function is similar to the Uniq command in UNIX. Before grouping, you need to use sorted () on the elements of the original circulator, sort by the key function, and let the same group of elements move closer to the position first.

Other Tools

compress (' ABCD ', [1, 1, 1, 0]) # Select the element in the first parameter ' ABCD ' according to the true and false values of [1, 1, 1, 0]. A, B, C

islice () # is similar to the slice () function, just returns a Circulator

Izip () # is similar to the zip () function, but only returns a circulator.

Summary

Itertools tools can be implemented on their own. Itertools just provides a more formed solution.

Welcome to the Python quick tutorial "

Python iterator package itertools (GO)