0
点赞
收藏
分享

微信扫一扫

numpy.size()函数


函数作用:计算当前数组中元素的总个数
函数调用方法:

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​​数组中的元素个数。

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


举报

相关推荐

0 条评论