简体   繁体   中英

Split numpy array object into two different size vectors using C

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...

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM