in c extension what is easiest way to build a (PyObject) list from an array of doubles?

Skip Montanaro skip at pobox.com
Tue Feb 10 21:51:41 CET 2004


    Chris> In c extension what is easiest way to build a (PyObject) list
    Chris> from an array of doubles?

    Chris> I don't think I can do "return Py_BuildValue(...) to make a list
    Chris> from an array can I???

If the length of the array is known when you write the code I think
something like this will work:

    return Py_BuildValue("[dddd]", a[0], a[1], a[2], a[3]);

    Chris> How else can I build and return a list??

Something like this (untested, no error checking):

    int alen = sizeof(a)/sizeof(a[0]);
    PyObject *list_of_floats = PyList_New(alen);
    for (i = 0; i < alen; i++) {
        PyList_SET_ITEM(list_of_floats, i, PyInt_FromLong(a[i]));
    }
    return list_of_floats;

Skip




More information about the Python-list mailing list