Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

The IterTools module is a built-in python iterator module used for iterator customization. This book continues to introduce the related content of finite iterators, and still introduces some common usage methods and application scenarios. In addition, because of many methods, so divided into two parts to be introduced.

1. Finite sequence iterators

1.1 groupby function

Groupby (iterable, key = None) :

Iterable: an iterable

Key: A function that takes elements of iterable as arguments

The selection of adjacent repeating elements in an iterator is done using the key function. As long as the two elements on the key function return the same value, they are considered a group. This function returns two values, the return value of the key function and an iterator for the adjacent elements of the same group

from itertools import groupby
​
for key, value in groupby('AaBbA', lambda x: x.upper()):
    print(f"key = {key}, value={list(value)}")
Copy the code

result:

key = A, value=['A', 'a']
key = B, value=['B', 'b']
key = A, value=['A']
Copy the code

1.2 islice function

islice(iterable, start, stop[, step]):

Iterable: an iterable

Start: indicates the start index

Stop: stops the index

Step: interval

Slicing iterable

from itertools import islice
​
for i in islice([0, 1, 2, 3, 4, 5], 1, 5, 2):
    print(i)
Copy the code

The result:

1
3
Copy the code

1.3 startmap function

startmap(iterable[, func]):

Iterable: an iterable

Func: a function that takes elements of iterable as arguments

Create an iterator where each value is func(*item) and the item comes from iterable. Similar to the map() function. The difference is that if func has multiple parameters, startMap uses variable parameters for func(*arg) and map uses positional parameters for func(arg1,arg2)

from itertools import starmap
​
values = [(0, 5), (1, 6), (2, 7), (3, 8), (4, 9)]
for i in starmap(lambda x, y: (x, y, x * y), values):
    print('%d * %d = %d' % i)
Copy the code

result:

0 * 5 = 0
1 * 6 = 6
2 * 7 = 14
3 * 8 = 24
4 * 9 = 36
Copy the code

1.4 takewhile function

Takewhile (predicate, iterable) :

Predicate: A predicate function whose return value is a bool

Iterable: an iterable

Predicate is determined by substituting the elements of iterable into the predicate, removing the result if it is False and retaining the result if it is True. Finally, a new iterator is generated. This is the opposite of the dropwhile() function

from itertools import takewhile
​
for i in takewhile(lambda x: x < 3, [1, 2, 3, 4, 5]):
    print(i)
Copy the code

result:

1
2
Copy the code

1.5 the tee function

tee(iterable,n=2)

Iterable: iterable object. N: Number of copies to be made.

Copy iterable to generate the same iterable of n.

from itertools import tee
​
for i in tee([1, 2, 3, 4, 5], 3):
    print(list(i))
Copy the code

result:

[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]Copy the code

1.6 zip_longest function

Zip returns a list of tuples. Izip_longest returns an iterator, so it is more efficient. When combining,izip_longest takes the longest sequence and the default complement bit is None.

zip_longest(*iterables,fillvalue=None)

From itertools import zip_longest print(list(zip_longest([1,2,3,4], 'ABC', 'xy', fillvalue='0'))Copy the code

result:

[(1, 'A', 'x'), (2, 'B', 'y'), (3, 'C', '0'), (4, '0', '0')]
Copy the code