双精度向量的Python swig包装向量显示为元组 我在C++中有一个函数返回一个向量对象。我已经使用Swig为Python包装了它。当我调用它时,我无法随后使用resize()或push\u back()向量方法修改函数的输出

双精度向量的Python swig包装向量显示为元组 我在C++中有一个函数返回一个向量对象。我已经使用Swig为Python包装了它。当我调用它时,我无法随后使用resize()或push\u back()向量方法修改函数的输出,c++,python,stl,swig,stdvector,C++,Python,Stl,Swig,Stdvector,当我尝试此操作时,我得到一个错误,即“tuple”对象没有“resize”或“push_back”属性。当我在Python中与向量交互时,Swig是否将向量转换为元组对象?如果是这样,那么我假设Python中这个函数的输出是不可变的,这是一个问题。我可以将这个对象传递到接受双精度向量向量向量的包装方法中。我就是不能用Python中的向量方法来处理它。请解释原因或提出解决办法 这是我的swig文件供参考。STL模板行朝向末端: /* SolutionCombiner.i */ %module So

当我尝试此操作时,我得到一个错误,即“tuple”对象没有“resize”或“push_back”属性。当我在Python中与向量交互时,Swig是否将向量转换为元组对象?如果是这样,那么我假设Python中这个函数的输出是不可变的,这是一个问题。我可以将这个对象传递到接受双精度向量向量向量的包装方法中。我就是不能用Python中的向量方法来处理它。请解释原因或提出解决办法

这是我的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();
/*SolutionCombiner.i*/
%模解组合器
%{
/*将头文件放在此处或函数声明如下*/
#包括“Coord.hpp”
#包括“MaterialData.hpp”
#包括“FailureCriteria.hpp”
#包括“QuadPointData.hpp”
#包括“ModelSolution.hpp”
#包括“排除区.hpp”
#包括“CriticalLocation.hpp”
#包括“FailureMode.hpp”
#包括“ExecutiveFunctions.hpp”
#包括
#包括
%}
%{
#使用_INIT定义SWIG_文件
std::ostream和new_流(const char*文件名){
返回*(新的std::of流(文件名));
}
std::istream和new_ifstream(常量字符*文件名){
返回*(新std::ifstream(文件名));
}
空写(std::ostream*FOUT,char*OutString){

*FOUT是的,向量模板返回一个不可变的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模板返回一个不可变的Python元组。也许您可以修改
std_vector.I
实现以返回列表,但选择它可能有一个很好的理由。您可以将它们转换为列表,以便在Pyt中操作它们尊敬的:

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