numpy.ma.ndenumerate#

ma.ndenumerate(a, compressed=True)[源]#

多维索引迭代器。

返回一个迭代器,生成数组坐标和值的对,并跳过被掩码的元素。当 compressed=False 时,ma.masked 将作为被掩码元素的值生成。此行为与 numpy.ndenumerate 的行为不同,后者会生成底层数据数组的值。

参数:
a类数组

一个(可能)包含掩码元素的数组。

compressed布尔值, 可选的

如果为 True(默认),则跳过被掩码的元素。

另请参阅

numpy.ndenumerate

忽略任何掩码的等效函数。

说明

版本 1.23.0 中的新功能。

示例

>>> import numpy as np
>>> a = np.ma.arange(9).reshape((3, 3))
>>> a[1, 0] = np.ma.masked
>>> a[1, 2] = np.ma.masked
>>> a[2, 1] = np.ma.masked
>>> a
masked_array(
  data=[[0, 1, 2],
        [--, 4, --],
        [6, --, 8]],
  mask=[[False, False, False],
        [ True, False,  True],
        [False,  True, False]],
  fill_value=999999)
>>> for index, x in np.ma.ndenumerate(a):
...     print(index, x)
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 1) 4
(2, 0) 6
(2, 2) 8
>>> for index, x in np.ma.ndenumerate(a, compressed=False):
...     print(index, x)
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) --
(1, 1) 4
(1, 2) --
(2, 0) 6
(2, 1) --
(2, 2) 8