10.10. Iterator Count
Learn more at https://docs.python.org/3/library/itertools.html
More information in Itertools
itertools.count(start=0, step=1)
10.10.1. Example
>>> from itertools import count
>>>
>>>
>>> result = count(3, 2)
>>>
>>> next(result)
3
>>>
>>> next(result)
5
>>>
>>> next(result)
7
10.10.2. Case Study
>>> import numpy as np
>>>
>>>
>>> a = np.arange(1,10).reshape(3,3)
>>>
>>> a
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>>
>>> a.flatten()
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>>
>>> a.itemsize
8
>>>
>>> a.strides
(24, 8)
>>>
>>> a.size
9
>>>
>>> a.size*a.itemsize
72
>>>
>>>
>>> value = count(0, 8)
>>> row = count(0, 24)
>>>
>>>
>>> for memaddr in row:
... print(memaddr)
10.10.3. Assignments
# %% About
# - Name: Generator Itertools Count
# - Difficulty: medium
# - Lines: 3
# - Minutes: 3
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Algorithm `Label encoder` encodes labels (str) to numbers (int).
# Each unique label will assign autoincremented numbers.
# example: {'virginica': 0, 'setosa': 1, 'versicolor': 2}
# 2. Modify code below and use `itertools.count()` instead of `i`
# 3. Function result must be `dict[str,int]`
# %% Polish
# 1. Algorytm `label_encoder` koduje etykiety (str) do liczb (int).
# Kolejnym wystąpieniom unikalnych etykiet przyporządkowuje liczby.
# przykład: {'virginica': 0, 'setosa': 1, 'versicolor': 2}
# 2. Zmodyfikuj kod poniżej i użyj `itertools.count()` zamiast `i`
# 3. Wynik funkcji ma być `dict[str,int]`
# %% Example
# >>> result
# {'virginica': 0,
# 'setosa': 1,
# 'versicolor': 2}
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> from inspect import isfunction, isgeneratorfunction
>>> assert result is not Ellipsis, \
'Assign result to variable: `result`'
>>> assert type(result) is dict, \
'Result must be a dict'
>>> assert len(result) > 0, \
'Result cannot be empty'
>>> assert all(type(element) is str for element in result), \
'All elements in result must be a str'
>>> from pprint import pprint
>>> pprint(result, sort_dicts=False, width=30)
{'virginica': 0,
'setosa': 1,
'versicolor': 2}
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`
# %% Imports
from itertools import count
# %% Types
result: dict[str, int]
# %% Data
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'),
]
# %% Result
result = {}
i = 0
for *_, species in DATA[1:]:
if species not in result:
result[species] = i
i += 1