如何返回数组c到Python? [英] How to return an C array to Python?

查看:335
本文介绍了如何返回数组c到Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写道,被称为被Python一个Python / C扩展功能,如何可以返回二维数组INT [] []到Python?

 静态的PyObject * inference_function(*的PyObject自我,*的PyObject参数)
{
    *的PyObject doc_lst;
    INT K,V;
    双α,β;
    INT n_iter;    如果(PyArg_ParseTuple(参数Oiiddi,&安培;!doc_lst,&安培; K,和放大器; V,&安培;α,和放大器;测试版,和放大器; n_iter))
    {
        的printf(传入参数错误\\ n!);
        返回NULL;
    }   返回Py_BuildValue(I,1);
}


解决方案

什么样的​​阵列您使用的是?一种方式,我觉得方便,使用numpy的数组,并在地方修改数据。 numpy的已经有很多关于操纵整型数组很大的操作,所以这是如果你想添加一些额外的功能得心应手。

第1步:将您的C扩展到numpy的

在Windows上,这是一样的东西。

 的#includeC:\\ Python34 /库/站点包/ numpy的/核心/有/ numpy的/ arrayobject.h

在OSX上它的东西像

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

第二步:抢指针数据这是非常容易的。

 为int * my_data_to_modify;
如果(PyArg_ParseTuple(参数,O,&安培; numpy_tmp_array)){
        / *指向我们的数据到numpy的像素阵列中的数据* /
        my_data_to_modify =(INT *)numpy_tmp_array->数据;
}... / *做有趣的事情与您的数据* /

2D numpy的数组用C

当你使用数据以这种方式工作,你可以分配它作为一个二维数组,例如

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

或全部为零,如果你想有一个空白的石板

不过,所有的C在乎的是连续的数据,这意味着你可以遍历它由一个行的lenght并修改它,仿佛它是一个二维数组

例如,如果你传递颜色RGB格式,例如,它们的100x3数组,你会考虑

  INT NUM_COLORS = numpy_tmp_array2->尺寸[0]; / *这给你列的长度* /
INT band_size = numpy_tmp_array2->尺寸[1]; / *这给你行的长度* /对于(i = 0; I< NUM_COLORS * band_size; I + = band_size){
    R = my_data [I]
    G = my_data第[i + 1];
    B = my_data第[i + 2];
}

要修改数据的地方,只是改变了数据阵列中的一个值。在Python的一面,numpy的阵列将有更改的值。

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);
}

解决方案

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.

这篇关于如何返回数组c到Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆