numpy.ndarray.flatten()函数

该函数主要用来快速扁平化数组,请看如下代码:

import numpy as np


class Debug:
    def __init__(self):
        self.array1 = np.array([[1, 2], [3, 4]])
        self.array2 = np.ones((2, 2, 2))

    def mainProgram(self):
        print("The value of array1 is: ")
        print(self.array1)
        print("The value of flattened array is: ")
        array2 = self.array1.flatten()
        print(array2)
        print("The value of array2 is: ")
        print(self.array2)
        print("The value of flattened array is: ")
        print(self.array2.flatten())


if __name__ == '__main__':
    main = Debug()
    main.mainProgram()
"""
The value of array1 is: 
[[1 2]
 [3 4]]
The value of flattened array is: 
[1 2 3 4]
The value of array2 is: 
[[[1. 1.]
  [1. 1.]]

 [[1. 1.]
  [1. 1.]]]
The value of flattened array is: 
[1. 1. 1. 1. 1. 1. 1. 1.]
"""

我们可以到,使用数组调用flatten()可以快速地将二维或者三维数组快速地扁平化。

如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~

猜你喜欢

转载自blog.csdn.net/u011699626/article/details/109005749
今日推荐