📜  查找一个数组元素的长度(以字节为单位)和 Numpy 中元素消耗的总字节数

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

查找一个数组元素的长度(以字节为单位)和 Numpy 中元素消耗的总字节数

在 NumPy 中,我们可以借助itemsize 找到一个字节中一个数组元素的长度。它将以整数形式返回数组的长度。在 numpy 中,借助nbytes 计算元素消耗的总字节数。

示例 1:

Python
import numpy as np
 
 
array = np.array([1,3,5], dtype=np.float64)
 
# Size of the array
print(array.size)
 
# Length of one array element in bytes,
print( array.itemsize)
 
# Total bytes consumed by the elements
# of the array
print(array.nbytes)


Python
import numpy as np
 
 
array = np.array([20, 34, 56, 78, 1, 9], dtype=np.float64)
 
# Size of the array
print(array.size)
 
# Length of one array element in bytes,
print(array.itemsize)
 
# Total bytes consumed by the elements
# of the array
print(array.nbytes)



输出:

3
8
24

示例 2:

Python

import numpy as np
 
 
array = np.array([20, 34, 56, 78, 1, 9], dtype=np.float64)
 
# Size of the array
print(array.size)
 
# Length of one array element in bytes,
print(array.itemsize)
 
# Total bytes consumed by the elements
# of the array
print(array.nbytes)


输出:

6
8
48