
Iterate a NumPy array in the for loop
A NumPy array can be iterated in the for statement like Python list.
import numpy
a = numpy.array([4, 5, 6, 7])
for n in a:
print(n)
# 4
# 5
# 6
# 7
If the array is a matrix or tensor, the arrays are shown in the iteration.
import numpy
a = numpy.array([[4, 5, 6], [7, 8, 9]])
for n in a:
print(n)
# [4 5 6]
# [7 8 9]
Comments
Powered by Markdown