An N-dimensional iterator object to index arrays. Given the shape of an array, an `ndindex` instance iterates over the N-dimensional index of the array. At each iteration a tuple of indices is returned, the last dimension is iterated over first. Parameters ---------- s
| 639 | |
| 640 | @set_module('numpy') |
| 641 | class ndindex: |
| 642 | """ |
| 643 | An N-dimensional iterator object to index arrays. |
| 644 | |
| 645 | Given the shape of an array, an `ndindex` instance iterates over |
| 646 | the N-dimensional index of the array. At each iteration a tuple |
| 647 | of indices is returned, the last dimension is iterated over first. |
| 648 | |
| 649 | Parameters |
| 650 | ---------- |
| 651 | shape : ints, or a single tuple of ints |
| 652 | The size of each dimension of the array can be passed as |
| 653 | individual parameters or as the elements of a tuple. |
| 654 | |
| 655 | See Also |
| 656 | -------- |
| 657 | ndenumerate, flatiter |
| 658 | |
| 659 | Examples |
| 660 | -------- |
| 661 | >>> import numpy as np |
| 662 | |
| 663 | Dimensions as individual arguments |
| 664 | |
| 665 | >>> for index in np.ndindex(3, 2, 1): |
| 666 | ... print(index) |
| 667 | (0, 0, 0) |
| 668 | (0, 1, 0) |
| 669 | (1, 0, 0) |
| 670 | (1, 1, 0) |
| 671 | (2, 0, 0) |
| 672 | (2, 1, 0) |
| 673 | |
| 674 | Same dimensions - but in a tuple ``(3, 2, 1)`` |
| 675 | |
| 676 | >>> for index in np.ndindex((3, 2, 1)): |
| 677 | ... print(index) |
| 678 | (0, 0, 0) |
| 679 | (0, 1, 0) |
| 680 | (1, 0, 0) |
| 681 | (1, 1, 0) |
| 682 | (2, 0, 0) |
| 683 | (2, 1, 0) |
| 684 | |
| 685 | """ |
| 686 | |
| 687 | def __init__(self, *shape): |
| 688 | if len(shape) == 1 and isinstance(shape[0], tuple): |
| 689 | shape = shape[0] |
| 690 | if min(shape, default=0) < 0: |
| 691 | raise ValueError("negative dimensions are not allowed") |
| 692 | self._iter = product(*map(range, shape)) |
| 693 | |
| 694 | def __iter__(self): |
| 695 | return self |
| 696 | |
| 697 | def __next__(self): |
| 698 | """ |
no outgoing calls
searching dependent graphs…