numpy.ma.ndenumerate#

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

多维索引迭代器。

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

参数:
a类数组对象

一个具有(可能)被掩码元素的数组。

compressedbool, optional

如果为 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