std::vector 到 boost::python::list
我有一个 C++ 方法,它从 python 调用,需要返回一个 python 列表对象。
我已经创建了该方法,并且它附加到一个公开的类并且现在可以从 python 调用......(它返回 void)。
所以问题是,如何从中创建一个 python 列表:
std::vector
我并没有真正理解此文档中构造函数的工作原理:
http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/list.html
另外...我真的不想返回某种包装的向量...我只是想创建一个新的python包含向量中的字符串值的列表。
如果这是重复的,我很抱歉...我发现了很多向量问题列表,但我找不到任何关于创建新的 python 列表的信息。
我可以扩展这个问题以包括其他一些问题,例如:
Creating a new pythondictionary from a: std::map
等等。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
boost::python
已经包含了包装矢量和地图的功能。这是向量的示例代码,您可以看到传递和返回列表都非常简单:地图与向量非常相似,并且在这篇文章中进行了描述:
Boost::Python- 可以自动从 dict 转换 - -> std::map?
不幸的是,
boost::python
当前不包含包装列表的功能。您可以手动创建包装器,但我没有时间回答这个问题。我可以今天或明天发布。我很乐意提出关于这个特定问题的新问题,因为答案将非常广泛,并且可能超出了本文的范围。我只是避免使用列表并使用向量。boost::python
already includes functionality for wrapping vectors and maps. Here's sample code for vectors, as you can see both passing and returning lists is quite simple:Maps are very similar to vectors and are described in this post:
Boost::Python- possible to automatically convert from dict --> std::map?
Unfortunately
boost::python
does not currently include facilities for wrapping lists. You can create the wrapper manually, but I'm out of time for this answer. I can post it today or tomorrow. I'd appreciate a new question about this particular problem, because the answer will be quite extensive and is probably outside of the scope of this post. I'd just avoid lists and use vectors instead.我有这个函数,使用迭代器将
std::vector
转换为py::list
:I have this function using iterators to convert
std::vector
topy::list
:如果您只想手动创建 python 列表(并且让函数返回 py::list 而不是向量),请按如下操作:
对于自动转换,定义从 python 列表到 c++ 以及从 c++ 到 python 列表的向量转换器 - - 我刚刚在实例化中写了相关内容shared_ptr 在 boost::python 中(回复的第二部分);这样,你就得到了真正的 python 列表。
自动转换的另一种可能性(我没有经验)是使用indexing_suite,它将
vector
包装为Python中的一个特殊类,正如一位同事已经在这里提到的那样。If you only want to create python list manually (and have the function return py::list rather than vector), do it like this:
For automatic conversions, define the converter for vector from python list to c++ and from c++ to python list -- I just wrote about that at Instantiating shared_ptr's in boost::python (the second part of the reply); that way, you get realy python lists.
Another possibility for automatic conversion (which I have no experience with) is to use indexing_suite, which will wrap
vector<string>
as a special class in python, as a colleague mentioned here already.来自 http://gist.github.com/octavifs/5362272:
From http://gist.github.com/octavifs/5362272:
FWIW,这是一个与 eudoxos 解决方案相同的模板化函数:
FWIW, here's a templated function in the same vein as eudoxos' solution:
我使用以下实用程序函数来转换 stl 容器。简单的 sum 函数说明了它们的使用方法。我还发现以下开源包有很多转换实用程序: https:// github.com/cctbx/cctbx_project/tree/master/scitbx/boost_python
I use following utility functions to convert from/to stl containers. The trivial sum function illustrates how they are used. Also I found following opensource package which has quite a few conversion utilities: https://github.com/cctbx/cctbx_project/tree/master/scitbx/boost_python