In a for i in iterator structure, the object returned by the looper will be given to i each time until the end of the loop. Using the iter() built-in function, we can turn containers such as tables and dictionaries into loopers. For example
for i in iter([2, 4, 5, 6]): print(i)
in the standard libraryitertools
package provides more flexible tools for generating loops. Most of the inputs to these tools are existing loopers. On the other hand, these tools can be implemented in Python itself, the package just provides a more standard and efficient implementation. This is in line with Python's philosophy that there are only and preferably only solutions.
# import the tools from itertools import *
1. Infinity Circulator
-
count(5, 2)
# An integer circulator starting at 5 and increasing by 2 each time, i.e. 5, 7, 9, 11, 13, 15 ... -
cycle('abc')
# Repeat the elements of the sequence both a, b, c, a, b, c ... -
repeat(1.2)
# Repeat 1.2 to form an infinite circulator, i.e. 1.2, 1.2, 1.2, ...
There can also be a limit on the number of times that repeat can be used: the
-
repeat(10, 5)
#Repeat 10 for a total of 5 repetitions
2. Functional tools
Functional programming is a programming paradigm that treats functions themselves as objects to be processed. In Python, functions are also objects, so it's easy to do some functional processing like the map(), filter(), reduce() functions.
itertools contains similar tools. These take functions as arguments and return the result as a looper.
For example:
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 functionally similar to the map() function, except that instead of a sequence, it returns a looper. Contains elements 1, 4, 27, i.e. the result of 1**1, 2**2, 3**3. The function pow (the built-in multiplication function) is given as the first argument. pow() acts on each element of the next two lists in turn and collects the results of the function to form the returned circulator.
In addition, the following functions can be used.
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 looper.
ifilter(lambda x: x > 5, [2, 3, 5, 6, 7]
Apply the lambda function to each element in turn and collect the original elements if the function returns True.6, 7
In addition.
ifilterfalse(lambda x: x > 5, [2, 3, 5, 6, 7])
Similar to above, but collects elements that return False. 2, 3, 5
takewhile(lambda x: x < 5, [1, 3, 6, 7, 1])
Collects elements to the looper when the function returns True. Once the function returns False, it stops. 1, 3
dropwhile(lambda x: x < 5, [1, 3, 6, 7, 1])
When the function returns False, skip the elements. Once the function returns True, it starts collecting all remaining elements into the looper.6, 7, 1
3. Combined tools
We can get a new looper by combining the original looper.
-
chain([1, 2, 3], [4, 5, 7])
# Connect two loopers into one. 1, 2, 3, 4, 5, 7 -
product('abc', [1, 2])
# The Cartesian product of a collection of multiple loopers. Equivalent to a nested loop
for m, n in product('abc', [1, 2]): print m, n
-
permutations('abc', 2)
# Pick two elements from 'abcd', say ab, bc, ... Sort all the results and return as a new looper.
Attention:The above combinations are split in order, i.e. ab, ba are returned.
-
combinations('abc', 2)
# Pick two elements from 'abcd', say ab, bc, ... Sort all the results and return as a new looper.
Attention:The above combinations are not sequential, i.e., only an ab is returned if ab, ba.
-
combinations_with_replacement('abc', 2)
# Similar to the above, but allows duplication of elements selected twice. I.e. more aa, bb, cc
4、groupby()
Apply the key function to the elements of the original looper. Based on the result of the key function, group the elements that have the same function result into a new looper. Each new looper is labeled with the result returned by the function.
This is like using the heights of a bunch of people as a looper. We could use a key function that returns "tall" if the height is greater than 180, "short" if the height is less than 160, and "middle" for the middle. Eventually, all heights will be categorized into three loopers, "tall", "short", and "middle".
def height_class(h): if h > 180: return "tall" elif h < 160: return "short" else: return "middle" friends = [191, 158, 159, 165, 170, 177, 181, 182, 190] friends = sorted(friends, key = height_class) for m, n in groupby(friends, key = height_class): print(m) print(list(n))
Attention:The function of groupby is similar to the uniq command in UNIX. Before grouping, you need to use sorted() to sort the elements of the original looper, according to the key function, so that the elements of the same group are close together in position first.
5. Other tools
-
compress('ABCD', [1, 1, 1, 0])
# Select the elements of the first parameter 'ABCD' according to the true and false values of [1, 1, 1, 0]. a, b, c -
islice()
# Similar to the slice() function, except that it returns a looper -
izip()
# Similar to the zip() function, except that it returns a looper.
Summary:
The tools of itertools can be implemented on their own. itertools just provides a more shaped solution.
to this article on the Python itertools looper itertools to this article, more related Python itertools content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!