简体   繁体   English

SWIG 将地图向量转换为 Python 字典列表

[英]SWIG convert vector of maps to python list of dictionaries

Hi I am working on wrapping C++ with SWIG for use in python.嗨,我正在研究用 SWIG 包装 C++ 以在 python 中使用。 Inorder to wrap C++ classes I am using SWIG.为了包装 C++ 类,我使用了 SWIG。 I have no good idea about typemaps and hence I am stuck.我对类型图没有什么好主意,因此我被卡住了。 I have a vector consisiting of multiple maps ie.我有一个由多个地图组成的向量,即。 std::vector<std::map<std::string, int>> and I want to return it from C++ function and convert it to list of dict in python. std::vector<std::map<std::string, int>> 并且我想从 C++ 函数返回它并将其转换为 python 中的 dict 列表。 I searched online but couldnot find anything.我在网上搜索,但找不到任何东西。

I have a class like this:我有一个这样的课程:

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;}

My interface file contains this我的接口文件包含这个


%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);
    }       
}

But when I call this module from python I get this:但是当我从 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>

You do need typemaps if you want the return value to be an actual Python list of Python dicts, but the included pre-defined templates might work for you.如果您希望返回值是 Python dict 的实际 Python 列表,则确实需要类型映射,但包含的预定义模板可能适合您。 Here's an example:下面是一个例子:

test.i:测试.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();

Use case:用例:

>>> 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> >)

It doesn't look pretty, but can be converted to Python list/dicts with:它看起来并不漂亮,但可以通过以下方式转换为 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}]

It can also still be accessed like a list or dict, even without conversion to print nicely as above:它仍然可以像列表或字典一样访问,即使没有像上面那样很好地打印:

>>> 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

The names used in the templates can be used to build objects to pass to C++ code as well:模板中使用的名称也可用于构建传递给 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}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM