如何传递 python 列表地址
How to pass python list address
我想将 c++ 代码转换为 python。我创建了一个使用 SWIG 的 python 模块来访问 c++ 类。
现在我想将以下 c++ 代码传递给 Python
C++
#define LEVEL 3
double thre[LEVEL] = { 1.0l, 1.0e+2, 1.0e+5 };
GradedDouble gd(LEVEL, thre);
gd.avg(thre);
我需要将上面的代码转换为python
C++用于生成 python 模块的构造函数
GradedDouble::GradedDouble(int n, double *thre)
{
n_ = n;
for (int i = 0; i < n_; ++i)
{
thre_.push_back(thre[i]);
grade_.push_back(new grade_type());
}
}
SWIG 接口文件
/* 文件: 例子.i */
%module example
%include typemaps.i
%apply double * INPUT { double *}
%apply double * INPUT { double *buf };
%{
#include "Item.h"
#include "GradedComplex.h"
#include "GradedDouble.h"
void avg(double *buf);
%}
%include <std_string.i>
%include <std_complex.i>
%include "Item.h"
%include "GradedComplex.h"
%include "GradedDouble.h"
%template(Int) Item<int>;
%template(Double) Item<double>;
%template(Complex) Item<std::complex<double> >;
示例 python 模块函数
class GradedDouble(_object):
__swig_setmethods__ = {}
__setattr__ = lambda self, name, value: _swig_setattr(self, GradedDouble, name, value)
__swig_getmethods__ = {}
__getattr__ = lambda self, name: _swig_getattr(self, GradedDouble, name)
__repr__ = _swig_repr
def __init__(self, *args):
this = _example.new_GradedDouble(*args)
try: self.this.append(this)
except: self.this = this
__swig_destroy__ = _example.delete_GradedDouble
__del__ = lambda self : None;
def push(self, *args): return _example.GradedDouble_push(self, *args)
def avg(self, *args): return _example.GradedDouble_avg(self, *args)
GradedDouble_swigregister = _example.GradedDouble_swigregister
GradedDouble_swigregister(GradedDouble)
如何转换它?
在SWIG中使用原始双*和长度作为接口更加困难。 您需要为参数对编写一个类型图,该类型图可以智能地读取给定大小的数组。 更易于使用SWIG的内置矢量支持。
示例,具有显示支持:
GradedDouble.i
%module GradedDouble
%{
#include "GradedDouble.h"
%}
%include <std_vector.i>
%include <std_string.i>
%template(DoubleVector) std::vector<double>;
%rename(__repr__) ToString;
%include "GradedDouble.h"
GradedDouble.h
#include <vector>
#include <string>
#include <sstream>
class GradedDouble
{
std::vector<double> thre_;
public:
GradedDouble(const std::vector<double>& dbls) : thre_(dbls) {}
~GradedDouble() {}
std::string ToString() const
{
std::ostringstream ss;
ss << "GradedDouble(";
for(auto d : thre_)
ss << ' ' << d;
ss << " )";
return ss.str();
}
};
输出
>>> import GradedDouble as gd
>>> x=gd.GradedDouble([1.2,3.4,5.6])
>>> x
GradedDouble( 1.2 3.4 5.6 )
执行此操作的另一种方法是使用 SWIG cpointer 和 carray。示例代码如下:
接口文件:
%module example
%include "cpointer.i"
%{
#include "Item.h"
#include "GradedDouble.h"
extern void avg(double *buf);
%}
%pointer_class(double, doublep);
%include "carrays.i"
%array_class(double, doubleArray);
%include <std_string.i>
%include "Item.h"
%include "GradedDouble.h"
%template(Double) Item<double>;
蟒蛇代码:
LEVEL = 3
thre = [1, 100, 10000]
a = example.doubleArray(LEVEL)
for i in xrange(LEVEL):
a[i] = thre[i]
gd=example.GradedDouble(LEVEL,a)
result = example.doublep()
gd.avg(result)
print result.value()
这对我有用
相关文章:
- 如何在 c++ stl 中获取列表中被推回的元素的地址,在常量时间内?
- Xcode:线程1:exc_bad_access(代码= 1,地址= 0x0)在缩短列表时
- C 给出了一个字符串列表,如何从类中获取各个变量地址
- 使用Boost库中的PC中获取我的以太网设备IP地址的列表
- 将结构元素地址设置为C 中的null(单链接列表)
- C++填充指向指针**列表的指针,该列表指向指针地址
- 链接列表和数组中的C 内存地址
- 链接列表错误 - 为什么头和尾引用指向相同地址
- C 错误:0x00934ABB(链接列表,地址簿)的未手动异常
- 如何传递 python 列表地址
- 列表类,其中填充了返回时丢失地址的指针
- 存储链接列表节点地址
- 变量参数列表:使用va_list或形式参数的地址
- 用于存储地址的单列列表的数据结构,更好地在C++中查找O(1)
- 将IP与主机/网络地址列表进行比较
- 是否可以获得多播组中每个套接字的所有sockaddr_in地址的列表
- 通过池分配器中的内存地址访问可用列表节点时出现问题
- 存储数组的地址列表,以解析原始的非终止文本
- 使用winapi获取本地IP地址列表
- 是否有一些Linux或gcc的特殊(内存)地址列表