问题描述
摘自此处:
typedef struct PyArrayObject {
PyObject_HEAD
char *data;
int nd;
npy_intp *dimensions;
npy_intp *strides;
PyObject *base;
PyArray_Descr *descr;
int flags;
PyObject *weakreflist;
} PyArrayObject;
当我查看numpy数组的规范时,我看不到它存储了数组中元素的数量.是真的吗?
When I look at the specification of a numpy array, I don't see that it stores number of elements of the array. Is that really the case?
不存储它的好处是什么?
What is the advantage of not storing that?
谢谢.
推荐答案
大小(即数组中元素的总数)计算为数组dimensions
中值的乘积.该数组的长度为nd
.
The size (that is, the total number of elements in the array) is computed as the product of the values in the array dimensions
. The length of that array is nd
.
在实现numpy核心的C代码中,您会发现宏PyArray_SIZE(obj)
的许多用法.这是该宏的定义:
In the C code that implements the core of numpy, you'll find many uses of the macro PyArray_SIZE(obj)
. Here's the definition of that macro:
#define PyArray_SIZE(m) PyArray_MultiplyList(PyArray_DIMS(m), PyArray_NDIM(m))
不存储它的好处是,不存储冗余数据.
The advantage of not storing it is, well, not storing redundant data.
这篇关于numpy是否在内部存储数组的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!