7.1. Array Iteration

7.1.1. 1-dimensional Array

>>> import numpy as np
>>>
>>>
>>> data = np.array([1, 2, 3])
>>>
>>> for value in data:
...     print(f'{value=}')
value=1
value=2
value=3

7.1.2. 2-dimensional Array

>>> import numpy as np
>>>
>>>
>>> data = np.array([[1, 2, 3],
...                  [4, 5, 6],
...                  [7, 8, 9]])
>>>
>>> for value in data:
...     print(f'{value=}')
value=array([1, 2, 3])
value=array([4, 5, 6])
value=array([7, 8, 9])
>>> import numpy as np
>>>
>>>
>>> data = np.array([[1, 2, 3],
...                  [4, 5, 6],
...                  [7, 8, 9]])
>>>
>>> for row in data:
...     for value in row:
...         print(f'{value=}')
value=1
value=2
value=3
value=4
value=5
value=6
value=7
value=8
value=9

7.1.3. Flat

Flatten:

>>> import numpy as np
>>>
>>>
>>> data = np.array([[1, 2, 3],
...                  [4, 5, 6],
...                  [7, 8, 9]])
>>>
>>> for value in data.flatten():
...     print(f'{value=}')
value=1
value=2
value=3
value=4
value=5
value=6
value=7
value=8
value=9

Ravel:

>>> import numpy as np
>>>
>>>
>>> data = np.array([[1, 2, 3],
...                  [4, 5, 6],
...                  [7, 8, 9]])
>>>
>>> for value in data.ravel():
...     print(f'{value=}')
value=1
value=2
value=3
value=4
value=5
value=6
value=7
value=8
value=9

7.1.4. Enumerate

>>> import numpy as np
>>>
>>>
>>> data = np.array([[1, 2, 3],
...                  [4, 5, 6],
...                  [7, 8, 9]])
>>>
>>> for i, value in enumerate(data):
...     print(f'{i=}, {value=}')
i=0, value=array([1, 2, 3])
i=1, value=array([4, 5, 6])
i=2, value=array([7, 8, 9])
>>> import numpy as np
>>>
>>>
>>> data = np.array([[1, 2, 3],
...                  [4, 5, 6],
...                  [7, 8, 9]])
>>>
>>> for i, value in enumerate(data.ravel()):
...     print(f'{i=}, {value=}')
i=0, value=1
i=1, value=2
i=2, value=3
i=3, value=4
i=4, value=5
i=5, value=6
i=6, value=7
i=7, value=8
i=8, value=9
>>> import numpy as np
>>>
>>>
>>> data = np.array([[1, 2, 3],
...                  [4, 5, 6],
...                  [7, 8, 9]])
>>>
>>> for i, row in enumerate(data):
...     for j, value in enumerate(row):
...         print(f'{i=}, {j=}, {value=}')
i=0, j=0, value=1
i=0, j=1, value=2
i=0, j=2, value=3
i=1, j=0, value=4
i=1, j=1, value=5
i=1, j=2, value=6
i=2, j=0, value=7
i=2, j=1, value=8
i=2, j=2, value=9

7.1.5. Assignments

Code 7.26. Solution
"""
* Assignment: Numpy Iteration
* Complexity: easy
* Lines of code: 3 lines
* Time: 5 min

English:
    1. Use `for` to iterate over `DATA`
    2. Define `result: list[int]` with even numbers from `DATA`
    3. Run doctests - all must succeed

Polish:
    1. Używając `for` iteruj po `DATA`
    2. Zdefiniuj `result: list[int]` z liczbami parzystymi z `DATA`
    3. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `number % 2 == 0`

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert type(result) is list, \
    'Variable `result` has invalid type, expected: list'
    >>> assert all(type(x) is np.int64 for x in result), \
    'All values in `result` must be type int'

    >>> result
    [2, 4, 6, 8]
"""

import numpy as np


DATA = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])


result = ...