numpy.ma.ndenumerate#

ma.ndenumerate(a, compressed=True)[原始碼]#

多維索引迭代器。

傳回一個迭代器,產生陣列坐標和值的配對,並跳過被遮罩的元素。若 compressed=False,則會產生 ma.masked 作為被遮罩元素的值。此行為與 numpy.ndenumerate 不同,後者會產生底層資料陣列的值。

參數:
aarray_like

一個可能包含遮罩元素的陣列。

compressedbool,選用

若為 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