Open In App

Display Numpy array in Fortran order

Last Updated : 17 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Fortran order/ array is a special case in which all elements of an array are stored in column-major order. Sometimes we need to display array in fortran order, for this numpy has a function known as numpy.nditer()

Syntax: numpy.nditer(op, flags=None, op_flags=None, op_dtypes=None, order=’K’, casting=’safe’, op_axes=None, itershape=None, buffersize=0)

Example 1:

Python3




# importing Numpy package
import numpy as np
 
# creating a Numpy array
num_array = np.arange(12).reshape(3, 4)
 
print("Array:")
print(num_array)
 
# Display array in Fortran order
# using numpy.nditer()
print("\nElements of the array in Fortran array:")
for num_array in np.nditer(num_array, order="F"):
    print(num_array, end=' ')


Output:

Array:
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

Elements of the array in Fortran array:
0 4 8 1 5 9 2 6 10 3 7 11

Example 2:

Python3




# importing Numpy package
import numpy as np
 
# creating a Numpy array
num_array = np.arange(12).reshape(2, 6)
     
print("Array:")
print(num_array)
 
# Display array in Fortran order
# using numpy.nditer()
print("\nElements of the array in Fortran array:")
for num_array in np.nditer(num_array, order="F"):
    print(num_array,end=' ')


Output:

Array:
[[ 0  1  2  3  4  5]
[ 6  7  8  9 10 11]]

Elements of the array in Fortran array:
0 6 1 7 2 8 3 9 4 10 5 11

Example 3:

Python3




# importing Numpy package
import numpy as np
 
# creating a Numpy array
num_array = np.arange(42).reshape(6, 7)
     
print("Array:")
print(num_array)
 
# Display array in Fortran order
# using numpy.nditer()
print("\nElements of the array in Fortran array:")
for num_array in np.nditer(num_array, order="F"):
    print(num_array,end=' ')


Output:

Array: [[ 0  1  2  3  4  5  6] [ 7  8  9 10 11 12 13] [14 15 16 17 18 19 20] [21 22 23 24 25 26 27] [28 29 30 31 32 33 34] [35 36 37 38 39 40 41]] Elements of the array in Fortran array: 0 7 14 21 28 35 1 8 15 22 29 36 2 9 16 23 30 37 3 10 17 24 31 38 4 11 18 25 32 39 5 12 19 26 33 40 6 13 20 27 34 41



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads