std :: vector to boost :: python :: list [英] std::vector to boost::python::list

查看:713
本文介绍了std :: vector to boost :: python :: list的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法在c + +,从python调用,并需要返回一个python列表对象。



我已经创建了该方法,问题是,如何从这里创建一个python列表:



std :: vector< std :: string>结果;



我不是真正了解如何构造函数从这个文档工作:



http://www.boost.org/doc/libs/ 1_37_0 / libs / python / doc / v2 / list.html



此外...我真的不想返回包装的矢量。 。我只是想用向量中的字符串值创建一个新的python列表。



我的道歉如果这是一个重复...我发现了很多列表但我找不到任何关于创建一个新的Python列表。



我可以扩展这个问题,包括一些其他问题,如:



从a: std :: map< std :: string,std :: string> 等创建一个新的python字典。 p>

解决方案

我有这个函数使用迭代器将 std :: vector code> py :: list :

  namespace py = boost :: python; 

template< class T>
py :: list std_vector_to_py_list(const std :: vector< T>& v)
{
py :: object get_iter = py :: iterator< std :: vector< T& >();
py :: object iter = get_iter(v);
py :: list l(iter);
return l;
}


I have a method in c++ that gets called from python and needs to return a python list object.

I have already created the method, and its attached to an exposed class and callable from python right now... (it returns void).

So the question is, how do I create a python list from this:

std::vector<std::string> results;

I am not really understanding how the constructor works from this documentation:

http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/list.html

Also... I don't really want to return kind of wrapped vector... I just want to create a new python list with the string values from the vector.

My apologies if this is a duplicate... I found quite a few list to vector questions but I couldn't find any about creating a new python list.

I could expand this question to include some other questions like:

Creating a new python dictionary from a: std::map<std::string, std::string> and so on.

解决方案

I have this function using iterators to convert std::vector to py::list:

namespace py = boost::python;

template<class T>
py::list std_vector_to_py_list(const std::vector<T>& v)
{
    py::object get_iter = py::iterator<std::vector<T> >();
    py::object iter = get_iter(v);
    py::list l(iter);
    return l;
}

这篇关于std :: vector to boost :: python :: list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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