SWIG将映射向量转换为python字典列表

2024-04-24 08:57:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用C++编写用C++编写的Python程序。为了包装C++类,我使用SWIG。我对打字图不太了解,因此我被卡住了。我有一个包含多个贴图的向量,即std::vector<;标准::地图<;标准::字符串,int>&燃气轮机;我想从C++函数中返回它,并将其转换为Python中的DICT列表。我在网上搜索了一下,但什么也没找到

我有一门课是这样的:

std::vector<std::map<std::string, int>> WapperCode::wrapper_fuc(){
std::vector<std::map<std::string, int>> outer_vector;
for(int i = 0 ; i < 10 ; i++){
    std::map<std::string, int> inner_dict;
    inner_dict.insert(std::pair<std::string, int>("first", 1));
    inner_dict.insert(std::pair<std::string, int>("second", 2));
    inner_dict.insert(std::pair<std::string, int>("third", 3));
    outer_vector.insert(outer_vector.begin() + i, inner_dict);
}
return outer_vector;}

我的接口文件包含以下内容


%module wrapper
%{
#include "wrapper.h"
#include <cstdio>
#include <cstdlib>
#include <sys/stat.h>
#include <map> 
#include <vector>
#define SWIG_PYTHON_STRICT_BYTE_CHAR // Swig is giving const char while wrapping bytes.
%}

%include "std_vector.i"
%include "std_map.i"
%include "std_pair.i"
%include "std_wstring.i" // Use this for abby wchar dependency
%include "std_string.i" // Get C++ string
%include "./lib/wrapper.h"


%typemap(out) std::vector< std::map< std::string,int,std::less< std::string >,std::allocator< std::pair< std::string const,int > > >,std::allocator< std::map< std::string,int,std::less< std::string >,std::allocator< std::pair< std::string const,int > > > > > & 
{
    for(int i = 0; i < $1->size(); ++i)
    {       
        int subLength = $1->data()[i].size();
        npy_intp dims[] = { subLength };
        PyObject* temp = PyArray_SimpleNewFromData(1, dims, NPY_INT, $1->data()[i].data());
        $result = SWIG_Python_AppendOutput($result, temp);
    }       
}

但是,当我从python调用此模块时,我得到以下结果:

Out[3]: <Swig Object of type 'std::vector< std::map< std::string,int,std::less< std::string >,std::allocator< std::pair< std::string const,int > > >,std::allocator< std::map< std::string,int,std::less< std::string >,std::allocator< std::pair< std::string const,int > > > > > *' at 0x7f4025d8e810>

Tags: mapstringincludewrapperdictintallocatorinner
1条回答
网友
1楼 · 发布于 2024-04-24 08:57:11

如果希望返回值是Python dict的实际Python列表,那么确实需要类型映射,但是包含的预定义模板可能适合您。下面是一个例子:

测试。i:

%module test

%{
#include <vector>
#include <map>
#include <string>

std::vector<std::map<std::string, int>> func() {
    std::vector<std::map<std::string, int>> outer_vector;
    for(int i = 0 ; i < 10 ; i++){
        std::map<std::string, int> inner_dict;
        inner_dict.insert(std::pair<std::string, int>("first", 1));
        inner_dict.insert(std::pair<std::string, int>("second", 2));
        inner_dict.insert(std::pair<std::string, int>("third", 3));
        outer_vector.insert(outer_vector.begin() + i, inner_dict);
    }
    return outer_vector;
}
%}

// These declare the templates
%include <std_vector.i>
%include <std_map.i>
%include <std_string.i>

// You must declare the template instances used so SWIG builds wrappers for them.
// Declare the inner templates before the outer ones.
%template(SiMap) std::map<std::string,int>;
%template(VectorSiMap) std::vector<std::map<std::string,int>>;

// Tell SWIG to wrap the function.
std::vector<std::map<std::string, int>> func();

用例:

>>> import test
>>> d=test.func()
>>> len(d)
10
>>> d
(<test.SiMap; proxy of <Swig Object of type 'std::map< std::string,int > *' at 0x0000028EA68E0B10> >, <test.SiMap; proxy of <Swig Object of type 'std::map< std::string,int > *' at 0x0000028EA712A570> >, <test.SiMap; proxy of <Swig Object of type 'std::map< std::string,int > *' at 0x0000028EA692A750> >, <test.SiMap; proxy of <Swig Object of type 'std::map< std::string,int > *' at 0x0000028EA7110750> >, <test.SiMap; proxy of <Swig Object of type 'std::map< std::string,int > *' at 0x0000028EA7110480> >, <test.SiMap; proxy of <Swig Object of type 'std::map< std::string,int > *' at 0x0000028EA71105A0> >, <test.SiMap; proxy of <Swig Object of type 'std::map< std::string,int > *' at 0x0000028EA71107E0> >, <test.SiMap; proxy of <Swig Object of type 'std::map< std::string,int > *' at 0x0000028EA7110840> >, <test.SiMap; proxy of <Swig Object of type 'std::map< std::string,int > *' at 0x0000028EA7110900> >, <test.SiMap; proxy of <Swig Object of type 'std::map< std::string,int > *' at 0x0000028EA71109F0> >)

它看起来并不漂亮,但可以通过以下方式转换为Python列表/目录:

>>> [dict(x) for x in d]
[{'first': 1, 'second': 2, 'third': 3}, {'first': 1, 'second': 2, 'third': 3}, {'first': 1, 'second': 2, 'third': 3}, {'first': 1, 'second': 2, 'third': 3}, {'first': 1, 'second': 2, 'third': 3}, {'first': 1, 'second': 2, 'third': 3}, {'first': 1, 'second': 2, 'third': 3}, {'first': 1, 'second': 2, 'third': 3}, {'first': 1, 'second': 2, 'third': 3}, {'first': 1, 'second': 2, 'third': 3}]

它也可以像列表或dict一样访问,即使不转换为如上所述的打印:

>>> d[0]
<test.SiMap; proxy of <Swig Object of type 'std::map< std::string,int > *' at 0x0000028EA68E0B10> >
>>> d[0]['first']
1
>>> d[1]['second']
2

模板中使用的名称也可以用来构建对象,以传递给C++代码:

>>> m = test.SiMap()
>>> m['first'] = 2
>>> v = test.VectorSiMap()
>>> v.push_back(m)
>>> v
<test.VectorSiMap; proxy of <Swig Object of type 'std::vector< std::map< std::string,int,std::less< std::string >,std::allocator< std::pair< std::string const,int > > > > *' at 0x0000028EA71381E0> >
>>> [dict(x) for x in v]
[{'first': 2}]

相关问题 更多 >