将Python字典转换成类似C的结构 [英] Convert Python dictionary into C like structure

查看:127
本文介绍了将Python字典转换成类似C的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和C的新手,我想知道如何将字典元素放入类似C的结构(struct)中.

I am a newbie in Python and C and I would like to know how to put dictionary elements into a C like structure (struct).

例如,这是我的结构:

typedef struct
{
    int dim;
    float *Tab1;
    float *Tab2;
}  
Tableaux;

这是我在Python中的字典:

Here is my dictionary in Python:

Tableaux = {}
Tableaux["dim"]=None
Tableaux["Tab1"]=[]
Tableaux["Tab2"]=[]

这是我的界面函数:

static PyObject* py_initTab(PyObject* self, PyObject* args)
{
    PyObject* dict;
    Tableaux Tab;
    if (!PyArg_ParseTuple(args, "O!", &dict))  
        return NULL;        

     Tab.Tab1=dict["Tab1"]; // How could I do something like that?      

     return Py_BuildValue("");
}

推荐答案

您可以使用 PyDict_GetItem() :

You can use PyDict_GetItem() for that:

PyObject* pytab1 = PyDict_GetItemString(dict, "Tab1");

由于结果是列表,因此您可以使用这些调用检查它

Since the result is a list, you can use these calls to examine it

本文档说明了如何转换基本类型在C和Python之间.

This documentation explains how to convert primitive types between C and Python.

这篇关于将Python字典转换成类似C的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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