函数作用:计算当前数组中元素的总个数
函数调用方法:
numpy.ndarray.size
代码如下:
import numpy as np
class NumpyStudy:
@staticmethod
def mainProgram():
array = np.array([[1, 2], [3, 4]])
# method 1
result1 = np.size(array)
# method 2
result2 = array.size
# method 3
result3 = np.product(array.shape)
print('数组array中包含的元素个数为:')
print(f"The value calculated by method 1: {result1}")
print(f"The value calculated by method 2: {result2}")
print(f"The value calculated by method 3: {result3}")
if __name__ == "__main__":
main = NumpyStudy()
main.mainProgram()
"""
数组array中包含的元素个数为:
The value calculated by method 1: 4
The value calculated by method 2: 4
The value calculated by method 3: 4
"""
我们可以看到,三种方法均可以获取到ndarray
数组中的元素个数。
码字不易,如果大家觉得有用,请高抬贵手给一个赞让我上推荐让更多的人看到吧~