NumPy package contains an iterator object numpy.nditer. It is an efficient multidimensional iterator object using which it is possible to iterate over an array. Each element of an array is visited using Python’s standard Iterator interface.
Let us create a 3X4 array using arange() function and iterate over it using nditer.
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print ('Original array is:')
print (a)
print ('\n')
print ('Modified array is:')
for x in np.nditer(a):
print (x)
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print ('Original array is:')
print (a)
print ('\n')
print ('Transpose of the original array is:')
b = a.T
print (b)
print ('\n')
print ('Modified array is:')
for x in np.nditer(b):
print (x)
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print ('Original array is:')
print (a)
print ('\n')
print ('Transpose of the original array is:')
b = a.T
print (b)
print ('\n')
print ('Sorted in C-style order:')
c = b.copy(order = 'C')
print (c)
for x in np.nditer(c):
print (x)
print ('\n')
print ('Sorted in F-style order:')
c = b.copy(order = 'F')
print (c)
for x in np.nditer(c):
print (x)
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print ('Original array is:')
print (a)
print ('\n')
print ('Sorted in C-style order:')
for x in np.nditer(a, order = 'C'):
print (x)
print ('\n')
print ('Sorted in F-style order:')
for x in np.nditer(a, order = 'F'):
print (x)
The nditer object has another optional parameter called op_flags. Its default value is read-only, but can be set to read-write or write-only mode. This will enable modifying array elements using this iterator.
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print ('Original array is:')
print (a)
print ('\n')
for x in np.nditer(a, op_flags = ['readwrite']):
x[...] = 2*x
print ('Modified array is:')
print (a)
The nditer class constructor has a ‘flags’ parameter, which can take the following values −
Parameter | Description |
---|---|
c_index | C_order index can be tracked |
f_index | Fortran_order index is tracked |
multi-index | Type of indexes with one per iteration can be tracked |
external_loop | Causes values given to be one-dimensional arrays with multiple values instead of zero-dimensional array |
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print ('Original array is:')
print (a)
print ('\n')
print ('Modified array is:')
for x in np.nditer(a, flags = ['external_loop'], order = 'F'):
print (x)
If two arrays are broadcastable, a combined nditer object is able to iterate upon them concurrently. Assuming that an array a has dimension 3X4, and there is another array b of dimension 1X4, the iterator of following type is used (array b is broadcast to size of a).
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print ('First array is:')
print (a)
print ('\n')
print ('Second array is:')
b = np.array([1, 2, 3, 4], dtype = int)
print (b)
print ('\n')
print ('Modified array is:')
for x,y in np.nditer([a,b]):
print ("%d:%d" % (x,y))
Thank You for visiting our website. If you like our work, please support us so that we can keep on creating new tutorials/blogs on interesting topics (like AI, ML, Data Science, Python, Digital Marketing, SEO, etc.) that can help people learn new things faster. You can support us by clicking on the Coffee button at the bottom right corner. We would appreciate even if you can give a thumbs-up to our article in the comments section below.
If you want to