Column address: a Python module per week

Meanwhile, welcome to follow my wechat official account AlwaysBeta, more exciting content waiting for you.

Python standard library module IterTools provides many convenient and flexible iterator tools, skilled use can greatly improve work efficiency.

Infinite iterator

itertools.count

count(start=0, step=1)
Copy the code

Creates an iterator that generates consecutive integers starting at n, or 0 if n is ignored. Example:

In [2] :for n initertools.count(): ... :if 100000 < n < 100010:
   ...:         printn ... :if n > 1000000:
   ...:         break. :100001
100002
100003
100004
100005
100006
100007
100008
100009
Copy the code

itertools.cycle

cycle(iterable)
Copy the code

To repeat an incoming sequence indefinitely. Example:

In [6]: count = 0

In [7] :for c in itertools.cycle("AB") :... :if count > 4:
   ...:         break. :printc ... : count +=1. : A B A B ACopy the code

itertools.repeat

repeat(object [,times])
Copy the code

Creates an iterator that repeatedly generates object, times (if provided) specifying a repeat count, and returns the object indefinitely if times is not provided. Example:

In [8] :for x in itertools.repeat("hello world".5) :... :printx ... : hello world hello world hello world hello world hello worldCopy the code

Functional tool

itertools.ifilter,itertools.reduce,itertools.imap,itertools.izip

Has the same functionality as the built-in filter(), reduce(), map(), zip() functions, except that it returns an iterator instead of a sequence. Removed in Python3 because the default built-in function is to return an iterator.

itertools.ifilterfalse

ifilterfalse(function or None, sequence)
Copy the code

Python3 as follows:

filterfalse(function or None, sequence)
Copy the code

Similar to filter, but only those items in the sequence whose function(item) is False are generated. Example:

In [25] :for elem in itertools.ifilterfalse(lambda x: x > 5[2.3.5.6.7) :... :printelem .... :2
3
5
Copy the code

itertools.izip_longest

izip_longest(iter1 [,iter2 [...]], [fillvalue=None])
Copy the code

Python3 as follows:

zip_longest(iter1 [,iter2 [...]], [fillvalue=None])
Copy the code

Similar to zip, but with the exception that it iterates through the longest iter, other iter iterates with fillValue if any value is missing. Example:

In [33] :for item in itertools.izip_longest('abcd'.'12', fillvalue=The '-'):
   ....:     printitem .... : ('a'.'1')
('b'.'2')
('c'.The '-')
('d'.The '-')
Copy the code

itertools.starmap

starmap(function, sequence)
Copy the code

Execute each element of the sequence sequence as a list of arguments to function, that is, function(*item), and return an iterator that executes the result. This function is valid only if the items generated by Iterable are suitable for this way of calling the function. Example:

In [35]: seq = [(0.5), (1.6), (2.7), (3.3), (3.8), (4.9)]

In [36] :for item in itertools.starmap(lambdax,y:(x, y, x*y), seq): ... :print "%d * %d = %d"% item ... :0 * 5 = 0
1 * 6 = 6
2 * 7 = 14
3 * 3 = 9
3 * 8 = 24
4 * 9 = 36
Copy the code

itertools.dropwhile

dropwhile(predicate, iterable)
Copy the code

Creates an iterator that dismisses the items in the iterable as long as the predicate(item) is True. If the predicate returns False, the items in the iterable and all subsequent items are generated. The first time after the condition is false returns the remaining items in the iterator. Example:

In [41] :for item in itertools.dropwhile(lambda x: x<1[- 1.0.1.2.3.4.1.2 -) :... :printitem ... :1
2
3
4
1
2 -
Copy the code

itertools.takewhile

takewhile(predicate, iterable)
Copy the code

The opposite of Dropwhile. Create an iterator that generates items in iterable where the predicate(item) is True, stopping as soon as the predicate evaluates to False. Example:

In [28] :for item in itertools.takewhile(lambda x: x < 2[- 1.0.1.2.3.4.1.2 -) :... :printitem .... :- 1
0
1
Copy the code

Combination of tools

itertools.chain

chain(*iterables)
Copy the code

To concatenate a group of iterators to form a larger iterator. Example:

In [9] :for c in itertools.chain('ABC'.'XYZ') :... :printc ... : A B C X Y ZCopy the code

itertools.product

product(*iterables, repeat=1)
Copy the code

Creates an iterator that generates the Cartesian product of multiple sets of iterators. The repeat parameter is used to specify the number of times the generated sequence is repeated. Example:

In [6] :for elem in itertools.product((1.2), ('a'.'b':))... :printelem ... : (1.'a')
(1.'b')
(2.'a')
(2.'b')

In [7] :for elem in itertools.product((1.2), ('a'.'b'), repeat=2) :... :printelem ... : (1.'a'.1.'a')
(1.'a'.1.'b')
(1.'a'.2.'a')
(1.'a'.2.'b')
(1.'b'.1.'a')
(1.'b'.1.'b')
(1.'b'.2.'a')
(1.'b'.2.'b')
(2.'a'.1.'a')
(2.'a'.1.'b')
(2.'a'.2.'a')
(2.'a'.2.'b')
(2.'b'.1.'a')
(2.'b'.1.'b')
(2.'b'.2.'a')
(2.'b'.2.'b')
Copy the code

itertools.permutations

permutations(iterable[, r])
Copy the code

Return an iterator of any r permutation tuple in iterable. If r is not specified, the length of the sequence is the same as the number of items in iterable. Example:

In [7] :for elem in itertools.permutations('abc'.2) :... :printelem ... : ('a'.'b')
('a'.'c')
('b'.'a')
('b'.'c')
('c'.'a')
('c'.'b')

In [8] :for elem in itertools.permutations('abc') :... :printelem ... : ('a'.'b'.'c')
('a'.'c'.'b')
('b'.'a'.'c')
('b'.'c'.'a')
('c'.'a'.'b')
('c'.'b'.'a')
Copy the code

itertools.combinations

combinations(iterable, r)
Copy the code

Mutations are similar to permutations, but the combination is arbitrary, meaning that if iterable is “ABC” and r is 2, ab and ba are considered repeated, and only ab is replaced. Example:

In [10] :for elem in itertools.combinations('abc'.2):
   ....:     printelem .... : ('a'.'b')
('a'.'c')
('b'.'c')
Copy the code

itertools.combinations_with_replacement

combinations_with_replacement(iterable, r)
Copy the code

This is similar to column but allows repeated values, so if iterable is “ABC” and r is 2, aa, bb, cc will be added. Example:

In [14] :for elem in itertools.combinations_with_replacement('abc'.2):
   ....:     printelem .... : ('a'.'a')
('a'.'b')
('a'.'c')
('b'.'b')
('b'.'c')
('c'.'c')
Copy the code

Other tools

itertools.compress

compress(data, selectors)
Copy the code

Equivalent to bool selection, the elements in data are retained only when the selectors’ corresponding element is true; otherwise, the elements are removed. Example:

In [39]: list(itertools.compress('abcdef'[1.1.0.1.0.1]))
Out[39] : ['a'.'b'.'d'.'f']

In [40]: list(itertools.compress('abcdef'[True.False.True]))
Out[40] : ['a'.'c']
Copy the code

itertools.groupby

groupby(iterable[, keyfunc])
Copy the code

Groups elements in iterable. Keyfunc is a grouping function used to group consecutive iterable items. If this is not specified, the iterable is grouped by default and returns a key, sub-iterator. Example:

In [45] :for key, value_iter in itertools.groupby('aaabbbaaccd'):
   ....:     printkey, list(value_iter) .... : a ['a'.'a'.'a']
b ['b'.'b'.'b']
a ['a'.'a']
c ['c'.'c']
d ['d']

In [48]: data = ['a'.'bb'.'cc'.'ddd'.'eee'.'f']

In [49] :for key, value_iter initertools.groupby(data, len): .... :printkey, list(value_iter) .... :1 ['a']
2 ['bb'.'cc']
3 ['ddd'.'eee']
1 ['f']
Copy the code

Note, note, note: You must sort before grouping, because groupby is grouped by comparing adjacent elements. If you look at the second example, because a and F are not grouped together, they don’t end up in the same list.

itertools.islice

islice(iterable, [start,] stop [, step])
Copy the code

Section selection, start is the start index, stop is the end index, step is the step size, start and step are optional. Example:

In [52]: list(itertools.islice([10.6.2.8.1.3.9].5))
Out[52] : [10.6.2.8.1]

In [53]: list(itertools.islice(itertools.count(), 6))
Out[53] : [0.1.2.3.4.5]

In [54]: list(itertools.islice(itertools.count(), 3.10))
Out[54] : [3.4.5.6.7.8.9]

In [55]: list(itertools.islice(itertools.count(), 3.10.2))
Out[55] : [3.5.7.9]
Copy the code

itertools.tee

tee(iterable, n=2)
Copy the code

Create n separate iterators from iterable, returned as tuples. Example:

In [57]: itertools.tee("abcedf")
Out[57]: (<itertools.tee at 0x7fed7b8f59e0>, <itertools.tee at 0x7fed7b8f56c8>)

In [58]: iter1, iter2 = itertools.tee("abcedf")

In [59]: list(iter1)
Out[59] : ['a'.'b'.'c'.'e'.'d'.'f']

In [60]: list(iter2)
Out[60] : ['a'.'b'.'c'.'e'.'d'.'f']

In [61]: itertools.tee("abcedf".3)
Out[61]:
(<itertools.tee at 0x7fed7b8f5cf8>,
 <itertools.tee at 0x7fed7b8f5cb0>,
 <itertools.tee at 0x7fed7b8f5b00>)
Copy the code

Related documents:

Blog. Konghy. Cn / 2017/04/25 /…

Juejin. Cn/post / 684490…