📜  以 Fortran 顺序显示 Numpy 数组

📅  最后修改于: 2022-05-13 01:55:39.219000             🧑  作者: Mango

以 Fortran 顺序显示 Numpy 数组

Fortran order/array 是一种特殊情况,其中数组的所有元素都以列优先顺序存储。有时我们需要以 fortran 顺序显示数组,因为这个 numpy 有一个称为numpy.nditer()的函数。

示例 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 Fortan array:")
for num_array in np.nditer(num_array, order="F"):
    print(num_array, end=' ')


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 Fortan array:")
for num_array in np.nditer(num_array, order="F"):
    print(num_array,end=' ')


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 Fortan array:")
for num_array in np.nditer(num_array, order="F"):
    print(num_array,end=' ')


输出:

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

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

示例 2:

蟒蛇3

# 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 Fortan array:")
for num_array in np.nditer(num_array, order="F"):
    print(num_array,end=' ')

输出:

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

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

示例 3:

蟒蛇3

# 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 Fortan array:")
for num_array in np.nditer(num_array, order="F"):
    print(num_array,end=' ')

输出: