NumPy - nditer迭代器

1 import numpy as np
2 a = np.arange(0,8).reshape((2,2,2))
3 print(a)

输出:

[[[0 1]
  [2 3]]

 [[4 5]
  [6 7]]]

 

使用多维数组迭代器nditer遍历输出每个元素的index和value,

1 it = np.nditer(a, flags=['multi_index'], order='C')
2 while not it.finished:
3     print('Index:%s, Value:%d' % (it.multi_index, a[it.multi_index]))
4     it.iternext()

输出:

Index:(0, 0, 0), Value:0
Index:(0, 0, 1), Value:1
Index:(0, 1, 0), Value:2
Index:(0, 1, 1), Value:3
Index:(1, 0, 0), Value:4
Index:(1, 0, 1), Value:5
Index:(1, 1, 0), Value:6
Index:(1, 1, 1), Value:7

 

posted @ 2021-09-10 21:02  岁月丶丿流年  阅读(214)  评论(0)    收藏  举报