SWIG将地图的矢量转换为字典的python列表



嗨,我正在用SWIG包装C++,以便在python中使用。为了包装C++类,我正在使用SWIG。我对打字图没有很好的想法,因此我陷入了困境。我有一个由多个映射组成的向量,即std::vector<std::映射<std::string,int>gt;我想从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>

如果您希望返回值是Python dicts的实际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列表/dicts

>>> [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}]

最新更新