[C++-sig] Arrays to python

Jim Bosch talljimbo at gmail.com
Thu Jan 21 22:17:08 CET 2010


On Thu, 2010-01-21 at 09:10 -0500, Ng, Enrico wrote:
> I had hoped that there was something simple after seeing the numeric part in the documentation.

Well, if you're content with a 1-D array and speed isn't a big issue,
you can just copy the elements of your array into a Python list like
this:

boost::python::list py_get_data() {
    boost::python::list r;
    for (int n=0; n<IMAGE_SIZE; ++n) {
        r.append(get_data()[n]);
    }
    return r;
}

If you want two dimensions, you could make a list-of-lists.
I don't think there's any way to get a C array into a Python object
without lots of explicit copying aside from using numpy.


Jim Bosch




More information about the Cplusplus-sig mailing list