2

I wrote a Python/C extension function that was called by Python, How can return an 2d array int[][] to Python?

static PyObject* inference_function(PyObject *self, PyObject *args)
{
    PyObject* doc_lst;
    int K,V;
    double alpha,beta;
    int n_iter;

    if (!PyArg_ParseTuple(args, "Oiiddi", &doc_lst, &K,&V, &alpha,&beta,&n_iter))
    {
        printf("传入参数错误!\n");
        return NULL;
    }

   return Py_BuildValue("i", 1);
}

1 Answer 1

3

What kind of array are you using? One way, which I find convenient, is to use numpy arrays, and modify the data in place. Numpy already has a lot of great operations for manipulating integer arrays, so this is handy if you're trying to add some additional functionality.

Step 1: link your C extension to numpy

on windows, this is something like

#include "C:\Python34/Lib/site-packages/numpy/core/include/numpy/arrayobject.h"

on osx it's something like

#include "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numpy/core/include/numpy/arrayobject.h"

Step 2: grab the pointer to the data. This is surprisingly easy

int* my_data_to_modify;
if (PyArg_ParseTuple(args, "O", &numpy_tmp_array)){
        /* Point our data to the data in the numpy pixel array */
        my_data_to_modify = (int*) numpy_tmp_array->data;
}

... /* do interesting things with your data */

2D numpy array in C

When you work with data this way, you can allocate it as a 2d array, e.g.

np.random.randint( 0, 100, (100,2) )

or all zeros if you want a blank slate

But all C cares about is contiguous data, which means you can loop through it by the lenght of a "row" and modify it as if it were a 2D array

for example, if you were passing in colors in rgb form, e.g., a 100x3 array of them, you would consider

int num_colors = numpy_tmp_array2->dimensions[0]; /* This gives you the column length */
int band_size = numpy_tmp_array2->dimensions[1]; /* This gives you the row length */

for ( i=0; i < num_colors * band_size; i += band_size ){
    r = my_data[i];
    g = my_data[i+1];
    b = my_data[i+2];
}

To modify the data in place, just change a value in the data array. On the Python side, the numpy array will have the changed value.

Sign up to request clarification or add additional context in comments.

4 Comments

create array by numpy.array then use in C code, good idea, let me try.
Be careful with stuff like strides and memory orders. Not all NumPy arrays are C-contiguous.
we can define macro to cast array to 2d arrays: #define CAST_ARRAY_TO_ARRAYS(array,arrays,m,n) arrays= malloc(sizeof(arrays) * m);if(arrays==NULL) exit(1);\ for (int i = 0; i < m; i++)\ {\ arrays[i] = &array[in];\ }
@lessisawesome did this answer wind up working for you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.