10.15. Iterator Itertools
Learn more at https://docs.python.org/library/itertools.html
More information in Itertools
from itertools import *
repeat(object[, times])
accumulate(iterable[, func, *, initial=None])
chain(*iterables)
compress(data, selectors)
islice(iterable, start, stop[, step])
starmap(function, iterable)
product(*iterables, repeat=1)
permutations(iterable, r=None)
combinations(iterable, r)
combinations_with_replacement(iterable, r)
groupby(iterable, key=None)
10.15.1. Itertools Accumulate
itertools.accumulate(iterable[, func, *, initial=None])
>>> from itertools import accumulate
>>>
>>>
>>> data = accumulate([1, 2, 3, 4])
>>>
>>> next(data)
1
>>> next(data)
3
>>> next(data)
6
>>> next(data)
10
>>> next(data)
Traceback (most recent call last):
StopIteration
10.15.2. Itertools Compress
itertools.compress(data, selectors)
:
>>> from itertools import compress
>>>
>>>
>>> # data = compress('ABCDEF', [1,0,1,0,1,1])
>>> data = compress('ABCDEF', [True, False, True, False, True, True])
>>>
>>> next(data)
'A'
>>> next(data)
'C'
>>> next(data)
'E'
>>> next(data)
'F'
>>> next(data)
Traceback (most recent call last):
StopIteration
10.15.3. Itertools Combinations
itertools.combinations(iterable, r)
>>> from itertools import combinations
>>>
>>>
>>> data = combinations([1, 2, 3, 4], 2)
>>>
>>> next(data)
(1, 2)
>>> next(data)
(1, 3)
>>> next(data)
(1, 4)
>>> next(data)
(2, 3)
>>> next(data)
(2, 4)
>>> next(data)
(3, 4)
>>> next(data)
Traceback (most recent call last):
StopIteration
10.15.4. Itertools Combinations With Replacement
itertools.combinations_with_replacement(iterable, r)
>>> from itertools import combinations_with_replacement
>>>
>>>
>>> data = combinations_with_replacement([1,2,3], 2)
>>>
>>> next(data)
(1, 1)
>>> next(data)
(1, 2)
>>> next(data)
(1, 3)
>>> next(data)
(2, 2)
>>> next(data)
(2, 3)
>>> next(data)
(3, 3)
>>> next(data)
Traceback (most recent call last):
StopIteration
10.15.5. Itertools GroupBy
itertools.groupby(iterable, key=None)
Make an iterator that returns consecutive keys and groups from the iterable. Generally, the iterable needs to already be sorted on the same key function. The operation of groupby() is similar to the uniq filter in Unix. It generates a break or new group every time the value of the key function changes. That behavior differs from SQL's GROUP BY which aggregates common elements regardless of their input order:
>>> from itertools import groupby
>>>
>>>
>>> data = groupby('AAAABBBCCDAABBB')
>>>
>>> next(data)
('A', <itertools._grouper object at 0x...>)
>>> next(data)
('B', <itertools._grouper object at 0x...>)
>>> next(data)
('C', <itertools._grouper object at 0x...>)
>>> next(data)
('D', <itertools._grouper object at 0x...>)
>>> next(data)
('A', <itertools._grouper object at 0x...>)
>>> next(data)
('B', <itertools._grouper object at 0x...>)
>>> next(data)
Traceback (most recent call last):
StopIteration
>>> [k for k, g in groupby('AAAABBBCCDAABBB')]
['A', 'B', 'C', 'D', 'A', 'B']
>>> [list(g) for k, g in groupby('AAAABBBCCD')]
[['A', 'A', 'A', 'A'], ['B', 'B', 'B'], ['C', 'C'], ['D']]
10.15.6. Use Case - 1
>>> from itertools import combinations
>>> from pprint import pprint
>>>
>>>
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue']
>>>
>>> result = combinations(colors, 3)
>>> pprint(list(result))
[('red', 'orange', 'yellow'),
('red', 'orange', 'green'),
('red', 'orange', 'blue'),
('red', 'yellow', 'green'),
('red', 'yellow', 'blue'),
('red', 'green', 'blue'),
('orange', 'yellow', 'green'),
('orange', 'yellow', 'blue'),
('orange', 'green', 'blue'),
('yellow', 'green', 'blue')]
class NumbersTest(unittest.TestCase):
def test_even(self):
"""
Test that numbers between 0 and 5 are all even.
"""
for i in range(0, 6):
with self.subTest(i=i):
self.assertEqual(i % 2, 0)
10.15.7. Use Case - 2
>>> from itertools import chain
>>> from pprint import pprint
>>>
>>>
>>> def powerset(iterable):
... """
... >>> powerset ([1,2,31])
... () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)
... """
... s = list(iterable)
... return chain.from_iterable(combinations(s,r) for r in range(len(s)+1))
...
>>>
>>> users = ['Alice', 'Bob', 'Carol', 'Dave']
>>>
>>> result = powerset(users)
>>> pprint(list(result))
[(),
('Alice',),
('Bob',),
('Carol',),
('Dave',),
('Alice', 'Bob'),
('Alice', 'Carol'),
('Alice', 'Dave'),
('Bob', 'Carol'),
('Bob', 'Dave'),
('Carol', 'Dave'),
('Alice', 'Bob', 'Carol'),
('Alice', 'Bob', 'Dave'),
('Alice', 'Carol', 'Dave'),
('Bob', 'Carol', 'Dave'),
('Alice', 'Bob', 'Carol', 'Dave')]
10.15.8. Use Case - 3
>>> from itertools import starmap
>>> from dataclasses import dataclass
>>>
>>>
>>> DATA = [
... ('sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species'),
... (5.8, 2.7, 5.1, 1.9, 'virginica'),
... (5.1, 3.5, 1.4, 0.2, 'setosa'),
... (5.7, 2.8, 4.1, 1.3, 'versicolor'),
... (6.3, 2.9, 5.6, 1.8, 'virginica'),
... (6.4, 3.2, 4.5, 1.5, 'versicolor'),
... (4.7, 3.2, 1.3, 0.2, 'setosa'),
... ]
>>>
>>>
>>> @dataclass
... class Iris:
... sl: float
... sw: float
... pl: float
... pw: float
... species: str
...
... def save(self):
... return 'success'
>>> result = starmap(Iris, DATA[1:])
>>>
>>> list(result)
[Iris(sl=5.8, sw=2.7, pl=5.1, pw=1.9, species='virginica'),
Iris(sl=5.1, sw=3.5, pl=1.4, pw=0.2, species='setosa'),
Iris(sl=5.7, sw=2.8, pl=4.1, pw=1.3, species='versicolor'),
Iris(sl=6.3, sw=2.9, pl=5.6, pw=1.8, species='virginica'),
Iris(sl=6.4, sw=3.2, pl=4.5, pw=1.5, species='versicolor'),
Iris(sl=4.7, sw=3.2, pl=1.3, pw=0.2, species='setosa')]
>>> result = starmap(Iris, DATA[1:])
>>> result = map(Iris.save, result)
>>>
>>> list(result)
['success', 'success', 'success', 'success', 'success', 'success']
>>> for iris in starmap(Iris, DATA[1:]):
... print(f'Saving to database...', end=' ')
... result = iris.save()
... print(result)
...
Saving to database... success
Saving to database... success
Saving to database... success
Saving to database... success
Saving to database... success
Saving to database... success
10.15.9. Use Case - 4
>>> from unittest import TestCase
>>> from itertools import product
>>>
>>>
>>> def parse(encoding, delimiter, lineterminator):
... return ...
>>>
>>>
>>> class ParserTest(TestCase):
... def test_parse(self):
... encoding = ['utf-8', 'cp1250', 'iso-8859-2', 'utf-16', 'utf-32']
... delimiter = [',', ';']
... lineterminator = ['\n', '\r\n']
... for option in product(encoding, delimiter, lineterminator):
... with self.subTest(option):
... result = parse(*option)
... self.assertEqual(result, ...)