c++ - double vector 的 Python swig-wrapped vector 显示为元组

标签 c++ python stl swig stdvector

我在 C++ 中有一个函数返回 vector<vector<double> >目的。我已经使用 Swig 为 Python 包装了它。当我调用它时,我无法随后使用 resize() 修改函数的输出。或 push_back() vector 方法。

当我尝试这个时,我得到一个错误,指出“元组”对象没有属性“resize”或“push_back”。当我在 Python 中与 Swig 交互时,Swig 是否会将 vector 转换为 Tuple 对象?如果是这样,那么我假设 Python 中此函数的输出是不可变的,这是一个问题。我可以将此对象传递给接受 double vector 的包装方法。我只是不能使用 Python 中的 vector 方法来处理它。任何关于为什么会这样的解释或关于解决方法的想法都将不胜感激。

这是我的 swig 文件以供引用。 STL 模板行接近尾声:

/* SolutionCombiner.i */
%module SolutionCombiner
%{
    /* Put header files here or function declarations like below */
    #include "Coord.hpp"
    #include "MaterialData.hpp"
    #include "FailureCriterion.hpp"
    #include "QuadPointData.hpp"
    #include "ModelSolution.hpp"
    #include "ExclusionZone.hpp"
    #include "CriticalLocation.hpp"
    #include "FailureMode.hpp"
    #include "ExecutiveFunctions.hpp"

    #include <fstream>
    #include <iostream> 
%}
%{
    #define SWIG_FILE_WITH_INIT
    std::ostream& new_ofstream(const char* FileName){
        return *(new std::ofstream(FileName));
    }

    std::istream& new_ifstream(const char* FileName){
        return *(new std::ifstream(FileName));
    }

    void write(std::ostream* FOUT, char* OutString){
        *FOUT << OutString;
    }

    std::ostream *get_cout(){return &std::cout;}
%}

%include "std_vector.i"
%include "std_string.i"
%include "std_set.i"
%include "../../source/Coord.hpp"
%include "../../source/MaterialData.hpp"
%include "../../source/FailureCriterion.hpp"
%include "../../source/QuadPointData.hpp"
%include "../../source/ModelSolution.hpp"
%include "../../source/ExclusionZone.hpp"
%include "../../source/CriticalLocation.hpp"
%include "../../source/FailureMode.hpp"
%include "../../source/ExecutiveFunctions.hpp"

namespace std {
   %template(IntVector) vector<int>;
   %template(DoubleVector) vector<double>;
   %template(DoubleVVector) vector<vector<double> >;
   %template(DoubleVVVector) vector<vector<vector<double> > >;
   %template(SolutionVector) vector<ModelSolution>;
   %template(CritLocVector) vector<CriticalLocation>;
   %template(CritLocVVector) vector<vector<CriticalLocation> >;
   %template(ModeVector) vector<FailureMode>;
   %template(IntSet) set<int>;
}
std::ofstream& new_ofstream(char* FileName);
std::ifstream& new_ifstream(char* FileName);
std::iostream *get_cout();

最佳答案

是的, vector 模板返回一个不可变的 Python 元组。也许你可以修改 std_vector.i返回列表的实现,但可能有一个很好的选择理由。您可以将它们转换为列表,以便您可以在 Python 中操作它们:

>>> x.func()
((1.5, 2.5, 3.5), (1.5, 2.5, 3.5), (1.5, 2.5, 3.5), (1.5, 2.5, 3.5))
>>> [list(n) for n in x.func()]
[[1.5, 2.5, 3.5], [1.5, 2.5, 3.5], [1.5, 2.5, 3.5], [1.5, 2.5, 3.5]]  

注意:我制作了一个返回 vector<vector<double>> 的示例函数作为测试。

关于c++ - double vector 的 Python swig-wrapped vector 显示为元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9595013/

相关文章:

python - 带有MQTT或Redis的Websockets?

c++ - 使用 tr1::shared_ptr 类型实现 std::equal

c++ - 为什么std::optional <bool>使用两个字节?

c++ - ComboBoxEx32 (CComboBoxEx) 键盘行为

c++ - 是否可以制作一个用分号替换换行符的宏

c++ - 如何从寄存器中提取值?

python - GDB打印STL数据

c++ - protected 成员与重载运算符冲突

python - PyGTK如何单击按钮并在弹出窗口中打开文件

python - 在 python 中为 while 循环设置 'else clause' 有什么好处?