使用Python / C API将Python列表传递给C函数

我最近开始使用Python / C API使用C代码为Python构建模块。 我一直试图将Python的数字列表传递给C函数但没有成功:

asdf_module.c

#include  #include  #include  #include  int _asdf(int low, int high, double *pr) // ... for (i = 0; i < range; i++) { printf("pr[%d] = %f\n", i, pr[i]); } // ... return 0; } static PyObject* asdf(PyObject* self, PyObject* args) { int low, high; double *pr; // maybe something about PyObject *pr ? if (!PyArg_ParseTuple(args, "iiO", &low, &high, &pr)) return NULL; // how to pass list pr to _asdf? return Py_BuildValue("i", _asdf(low, high, pr)); } static PyMethodDef AsdfMethods[] = { {"asdf", asdf, METH_VARARGS, "..."}, {NULL, NULL, 0, NULL} }; PyMODINIT_FUNC initasdf(void) { (void) Py_InitModule("asdf", AsdfMethods); } 

使用setup.py构建模块:

 from distutils.core import setup, Extension module1 = Extension('asdf', sources = ['asdf_module.c']) setup (name = 'asdf', version = '1.0', description = 'This is a demo package', ext_modules = [module1]) 

使用test_module.py中的模块:

 import asdf print asdf.asdf(-2, 3, [0.7, 0.0, 0.1, 0.0, 0.0, 0.2]) 

但是,我得到的输出是:

pr [0] = 0.000000

pr [1] = 0.000000

pr [2] = 0.000000

pr [3] = 0.000000

pr [4] = 0.000000

pr [5] = -nan

另外,代替_asdf返回0,它如何返回n值的数组(其中n是固定数字)?

这个例子将告诉你如何

  1. 获取列表的长度
  2. double数组分配存储空间
  3. 获取列表的每个元素
  4. 将每个元素转换为double
  5. 将每个转换后的元素存储在数组中

这是代码:

 #include "Python.h" int _asdf(double pr[], int length) { for (int index = 0; index < length; index++) printf("pr[%d] = %f\n", index, pr[index]); return 0; } static PyObject *asdf(PyObject *self, PyObject *args) { PyObject *float_list; int pr_length; double *pr; if (!PyArg_ParseTuple(args, "O", &float_list)) return NULL; pr_length = PyObject_Length(float_list); if (pr_length < 0) return NULL; pr = (double *) malloc(sizeof(double *) * pr_length); if (pr == NULL) return NULL; for (int index = 0; index < pr_length; index++) { PyObject *item; item = PyList_GetItem(float_list, index); if (!PyFloat_Check(item)) pr[index] = 0.0; pr[index] = PyFloat_AsDouble(item); } return Py_BuildValue("i", _asdf(pr, pr_length)); } 

注意:删除了空格和大括号以防止代码滚动。

测试程序

 import asdf print asdf.asdf([0.7, 0.0, 0.1, 0.0, 0.0, 0.2]) 

产量

 pr[0] = 0.700000 pr[1] = 0.000000 pr[2] = 0.100000 pr[3] = 0.000000 pr[4] = 0.000000 pr[5] = 0.200000 0