3.11. Array Iteration

3.11.1. 1-dimensional Array

import numpy as np


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

for element in a:
    print(element)

# 1
# 2
# 3

3.11.2. 2-dimensional Array

import numpy as np


a = np.array([[1, 2, 3],
              [4, 5, 6]])

for element in a:
    print(element)

# [1 2 3]
# [4 5 6]
import numpy as np


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

for element in a:
    print(element)

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

3.11.3. Assignments

3.11.3.1. Iteration

English
  1. Given a: ndarray (see below)

  2. Iterate over a

  3. Print even numbers

Polish
  1. Dany a: ndarray (patrz poni┼╝ej)

  2. Iteruj po a

  3. Wypisz liczby parzyste

Input
a = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])
The whys and wherefores
  • Iterating 2-dimensional ndarray

Hint

element % 2 == 0