使用 C 将 numpy 数组对象拆分为两个不同大小的向量
我有
X 作为输入 --- 这是 dtype 对象 这是以下结构 x=[[1,2,3,4...n 个元素],[1 个元素],[1,2,...m 个元素],[1 个元素]]
模拟输入...
>>> from numpy import *
>>> x=array([array([1,2,3,4,5]),array([1]),array([1,2,3,4,5,6,7,8]),array([1])],dtype=object)
>>> x
array([[1 2 3 4 5], [1], [1 2 3 4 5 6 7 8], [1]], dtype=object)
我传递 X作为我的 Python C 扩展的参数 PyArray_Object
static PyObject* samp(PyObject *self, PyObject *args) {
PyArrayObject *array,*p1,*p2;
int n,j;
if (!PyArg_ParseTuple(args, "O!",&PyArray_Type, &array))
return NULL;
n=array->nd;
if(n!=1 || array->descr->type_num!=PyArray_OBJECT) {
PyErr_SetString(PyExc_ValueError, "array must be one-dimensional and of Object type");
return NULL;
}
j=array->dimensions[0];
/* ...... */
}
现在我被困在这里,因为我不知道如何将其拆分为 4 个对象 请任何人能给我一些关于这个的指示......
I have
X as input --- this is dtype object
this is of following structure
x=[[1,2,3,4...n elements],[1 element],[1,2,...m elements],[1 element]]
To mimic the input...
>>> from numpy import *
>>> x=array([array([1,2,3,4,5]),array([1]),array([1,2,3,4,5,6,7,8]),array([1])],dtype=object)
>>> x
array([[1 2 3 4 5], [1], [1 2 3 4 5 6 7 8], [1]], dtype=object)
I passing X as an argument to my Python C extension as PyArray_Object
static PyObject* samp(PyObject *self, PyObject *args) {
PyArrayObject *array,*p1,*p2;
int n,j;
if (!PyArg_ParseTuple(args, "O!",&PyArray_Type, &array))
return NULL;
n=array->nd;
if(n!=1 || array->descr->type_num!=PyArray_OBJECT) {
PyErr_SetString(PyExc_ValueError, "array must be one-dimensional and of Object type");
return NULL;
}
j=array->dimensions[0];
/* ...... */
}
Now I am stuck here as I m not sure how to split this into 4 objects
Please kindly can anyone give me few pointers on this...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最初你有一个由四个数组组成的数组。以下行将其提取为四个数组变量:
a,b,c,d=x[0],x[1],x[2],x[3]
如果您需要本机 Python 对象使用列表理解代替 numpy 数组:
objs = [y for y in a]
Originally you had an array of four arrays. The following line extracts it into four array variables:
a,b,c,d=x[0],x[1],x[2],x[3]
If you need native Python objects instead of numpy arrays, use list comprehension:
objs = [y for y in a]