小编M. *_* D.的帖子

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

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

asdf_module.c

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <Python.h>

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, …
Run Code Online (Sandbox Code Playgroud)

c python

4
推荐指数
1
解决办法
1635
查看次数

标签 统计

c ×1

python ×1