pytutorial/numpy/ndenumerate
David Rotermund e6ee924a22
Create README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
2023-12-30 17:00:30 +01:00
..
README.md Create README.md 2023-12-30 17:00:30 +01:00

ndenumerate

{:.no_toc}

* TOC {:toc}

Top

Questions to David Rotermund

numpy.ndenumerate

class numpy.ndenumerate(arr)

Multidimensional index iterator.

Return an iterator yielding pairs of array coordinates and values.

import numpy as np

a = np.arange(0, 12).reshape((4, 3))
print(a)
print()

for index, x in np.ndenumerate(a):
    print(index, x)

Output:

[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]

(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
(3, 0) 9
(3, 1) 10
(3, 2) 11