Multidimensional index iterator. Return an iterator yielding pairs of array coordinates and values. Parameters ---------- arr : ndarray Input array. See Also -------- ndindex, flatiter Examples -------- >>> import numpy as np >>> a = np.arra
| 589 | |
| 590 | @set_module('numpy') |
| 591 | class ndenumerate: |
| 592 | """ |
| 593 | Multidimensional index iterator. |
| 594 | |
| 595 | Return an iterator yielding pairs of array coordinates and values. |
| 596 | |
| 597 | Parameters |
| 598 | ---------- |
| 599 | arr : ndarray |
| 600 | Input array. |
| 601 | |
| 602 | See Also |
| 603 | -------- |
| 604 | ndindex, flatiter |
| 605 | |
| 606 | Examples |
| 607 | -------- |
| 608 | >>> import numpy as np |
| 609 | >>> a = np.array([[1, 2], [3, 4]]) |
| 610 | >>> for index, x in np.ndenumerate(a): |
| 611 | ... print(index, x) |
| 612 | (0, 0) 1 |
| 613 | (0, 1) 2 |
| 614 | (1, 0) 3 |
| 615 | (1, 1) 4 |
| 616 | |
| 617 | """ |
| 618 | |
| 619 | def __init__(self, arr): |
| 620 | self.iter = np.asarray(arr).flat |
| 621 | |
| 622 | def __next__(self): |
| 623 | """ |
| 624 | Standard iterator method, returns the index tuple and array value. |
| 625 | |
| 626 | Returns |
| 627 | ------- |
| 628 | coords : tuple of ints |
| 629 | The indices of the current iteration. |
| 630 | val : scalar |
| 631 | The array element of the current iteration. |
| 632 | |
| 633 | """ |
| 634 | return self.iter.coords, next(self.iter) |
| 635 | |
| 636 | def __iter__(self): |
| 637 | return self |
| 638 | |
| 639 | |
| 640 | @set_module('numpy') |
no outgoing calls
searching dependent graphs…