# 3.1. Array Iteration¶

## 3.1.1. 1-dimensional Array¶

import numpy as np

data = np.array([1, 2, 3])

for value in data:
print(value)

# 1
# 2
# 3


## 3.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(value)

# [1 2 3]
# [4 5 6]
# [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(value)

# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9


## 3.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(value)

# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9


Ravel:

import numpy as np

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

for value in data.ravel():
print(value)

# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
# 9


## 3.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(i, value)

# 0 [1 2 3]
# 1 [4 5 6]
# 2 [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(i, value)
# 0 1
# 1 2
# 2 3
# 3 4
# 4 5
# 5 6
# 6 7
# 7 8
# 8 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(i, j, value)

# 0 0 1
# 0 1 2
# 0 2 3
# 1 0 4
# 1 1 5
# 1 2 6
# 2 0 7
# 2 1 8
# 2 2 9


## 3.1.5. Assignments¶

"""
* 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 = ...