MCPcopy Create free account
hub / github.com/numpy/numpy / ndenumerate

Class ndenumerate

numpy/lib/index_tricks.py:570–615  ·  view source on GitHub ↗

Multidimensional index iterator. Return an iterator yielding pairs of array coordinates and values. Parameters ---------- arr : ndarray Input array. See Also -------- ndindex, flatiter Examples -------- >>> a = np.array([[1, 2], [3, 4]]) >>>

Source from the content-addressed store, hash-verified

568
569@set_module('numpy')
570class ndenumerate:
571 """
572 Multidimensional index iterator.
573
574 Return an iterator yielding pairs of array coordinates and values.
575
576 Parameters
577 ----------
578 arr : ndarray
579 Input array.
580
581 See Also
582 --------
583 ndindex, flatiter
584
585 Examples
586 --------
587 >>> a = np.array([[1, 2], [3, 4]])
588 >>> for index, x in np.ndenumerate(a):
589 ... print(index, x)
590 (0, 0) 1
591 (0, 1) 2
592 (1, 0) 3
593 (1, 1) 4
594
595 """
596
597 def __init__(self, arr):
598 self.iter = np.asarray(arr).flat
599
600 def __next__(self):
601 """
602 Standard iterator method, returns the index tuple and array value.
603
604 Returns
605 -------
606 coords : tuple of ints
607 The indices of the current iteration.
608 val : scalar
609 The array element of the current iteration.
610
611 """
612 return self.iter.coords, next(self.iter)
613
614 def __iter__(self):
615 return self
616
617
618@set_module('numpy')

Callers 2

test_basicMethod · 0.90
test_ndindexFunction · 0.90

Calls

no outgoing calls

Tested by 2

test_basicMethod · 0.72
test_ndindexFunction · 0.72