![](/img/trans.png)
[英]Fails convertion from std::vector<double> to Python list (boost python)
[英]std::vector to boost::python::list
我在 c++ 中有一個從 python 調用的方法,需要返回 python 列表 ZA8CFDE6331BD59EB2ACZ6F8911C4B666
我已經創建了該方法,並將其附加到暴露的 class 並且現在可以從 python 調用......(它返回 void)。
所以問題是,我如何從中創建一個 python 列表:
std::vector<std::string> results;
我不是很了解構造函數是如何從這個文檔中工作的:
http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/list.html
另外...我真的不想返回一種包裝的向量...我只想使用向量中的字符串值創建一個新的 python 列表。
如果這是重復的,我深表歉意......我找到了很多矢量問題列表,但我找不到任何關於創建新 python 列表的信息。
我可以擴展這個問題以包括其他一些問題,例如:
從以下位置創建一個新的 python 字典: std::map<std::string, std::string>
等等。
boost::python
已經包含了包裝向量和地圖的功能。 這是向量的示例代碼,您可以看到傳遞和返回列表都非常簡單:
// C++ code
typedef std::vector<std::string> MyList;
class MyClass {
MyList myFuncGet();
void myFuncSet(const Mylist& list);
// stuff
};
// Wrapper code
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(mymodule)
{
class_<MyList>("MyList")
.def(vector_indexing_suite<MyList>() );
class_<MyClass>("MyClass")
.def("myFuncGet", &MyClass::myFuncGet)
.def("myFuncSet", &MyClass::myFuncSet)
;
}
地圖與向量非常相似,並在這篇文章中進行了描述: Boost::Python- possible to automatically convert from dict --> std::map?
不幸的是boost::python
目前不包括包裝列表的設施。 您可以手動創建包裝器,但我沒有時間回答這個問題。 我可以在今天或明天發布。 我很感激關於這個特定問題的新問題,因為答案將非常廣泛,並且可能不在本文的 scope 范圍內。 我只是避免使用列表並使用向量。
我有這個 function 使用迭代器將std::vector
轉換為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;
}
如果您只想手動創建 python 列表(並讓 function 返回 py::list 而不是向量),請執行以下操作:
/* using namespace std; namespace py=boost::python;
#define FOREACH BOOST_FOREACH
*/
vector<string> ss;
py::list ret;
FOREACH(const string& s, ss) ret.append(s);
return s;
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); 這樣,你得到真正的 python 列表。
自動轉換的另一種可能性(我沒有經驗)是使用 indexing_suite,它會將vector<string>
包裝為 python 中的特殊 class,正如這里已經提到的同事一樣。
來自http://gist.github.com/octavifs/5362272 :
// Converts a C++ vector to a python list
template <class T>
boost::python::list toPythonList(std::vector<T> vector) {
typename std::vector<T>::iterator iter;
boost::python::list list;
for (iter = vector.begin(); iter != vector.end(); ++iter) {
list.append(*iter);
}
return list;
}
FWIW,這是一個模板化的 function,與 eudoxos 的解決方案相同:
namespace py = boost::python;
template<class T>
py::list std_vector_to_py_list(const std::vector<T>& v)
{
py::list l;
typename std::vector<T>::const_iterator it;
for (it = v.begin(); it != v.end(); ++it)
l.append(*it);
return l;
}
我使用以下實用程序函數在 stl 容器之間進行轉換。 簡單的總和 function 說明了它們是如何使用的。 我還發現以下開源 package 有很多轉換實用程序: https://github.com/cctbx/cctbx_project/tree/master/scitbx/boost_python
#include <vector>
#include <boost/python.hpp>
#include <boost/python/object.hpp>
#include <boost/python/stl_iterator.hpp>
namespace bpy = boost::python;
namespace fm {
template <typename Container>
bpy::list stl2py(const Container& vec) {
typedef typename Container::value_type T;
bpy::list lst;
std::for_each(vec.begin(), vec.end(), [&](const T& t) { lst.append(t); });
return lst;
}
template <typename Container>
void py2stl(const bpy::list& lst, Container& vec) {
typedef typename Container::value_type T;
bpy::stl_input_iterator<T> beg(lst), end;
std::for_each(beg, end, [&](const T& t) { vec.push_back(t); });
}
bpy::list sum(const bpy::list& lhs, const bpy::list& rhs) {
std::vector<double> lhsv;
py2stl(lhs, lhsv);
std::vector<double> rhsv;
py2stl(rhs, rhsv);
std::vector<double> result(lhsv.size(), 0.0);
for (int i = 0; i < lhsv.size(); ++i) {
result[i] = lhsv[i] + rhsv[i];
}
return stl2py(result);
}
} // namespace fm
BOOST_PYTHON_MODULE(fm)
{
bpy::def("sum", &fm::sum);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.