我有
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个对象 请大家多多指点一下......
答案 0 :(得分:1)
最初你有一个包含四个数组的数组。以下行将其提取为四个数组变量:
a,b,c,d=x[0],x[1],x[2],x[3]
如果您需要本机Python对象而不是numpy数组,请使用list comprehension:
objs = [y for y in a]