https://github.com/tdegeus/pybind11_examples/blob/master/01_py-list_cpp-vector/test.py

import exampleA = [1.,2.,3.,4.]B = example.modify(A)print(B)

pybind传输list

感觉数据类型必须是单一的某个类型:

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>// ----------------
// Regular C++ code
// ----------------// multiply all entries by 2.0
// input:  std::vector ([...]) (read only)
// output: std::vector ([...]) (new copy)
std::vector<double> modify(const std::vector<double>& input)
{std::vector<double> output;std::transform(input.begin(),input.end(),std::back_inserter(output),[](double x) -> double { return 2.*x; });// N.B. this is equivalent to (but there are also other ways to do the same)//// std::vector<double> output(input.size());//// for ( size_t i = 0 ; i < input.size() ; ++i )//   output[i] = 2. * input[i];return output;
}// ----------------
// Python interface
// ----------------namespace py = pybind11;PYBIND11_MODULE(example,m)
{m.doc() = "pybind11 example plugin";m.def("modify", &modify, "Multiply all entries of a list by 2.0");
}

例子2:

import numpy as np
import exampleA = [0,1,2,3,4,5]
B = example.multiply(A)print('input list = ',A)
print('output     = ',B)A = np.arange(10)
B = example.multiply(A)print('input list = ',A)
print('output     = ',B)
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include <vector>// -------------
// pure C++ code
// -------------std::vector<int> multiply(const std::vector<double>& input)
{std::vector<int> output(input.size());for ( size_t i = 0 ; i < input.size() ; ++i )output[i] = 10*static_cast<int>(input[i]);return output;
}// ----------------
// Python interface
// ----------------namespace py = pybind11;// wrap C++ function with NumPy array IO
py::array_t<int> py_multiply(py::array_t<double, py::array::c_style | py::array::forcecast> array)
{// allocate std::vector (to pass to the C++ function)std::vector<double> array_vec(array.size());// copy py::array -> std::vectorstd::memcpy(array_vec.data(),array.data(),array.size()*sizeof(double));// call pure C++ functionstd::vector<int> result_vec = multiply(array_vec);// allocate py::array (to pass the result of the C++ function to Python)auto result        = py::array_t<int>(array.size());auto result_buffer = result.request();int *result_ptr    = (int *) result_buffer.ptr;// copy std::vector -> py::arraystd::memcpy(result_ptr,result_vec.data(),result_vec.size()*sizeof(int));return result;
}// wrap as Python module
PYBIND11_MODULE(example,m)
{m.doc() = "pybind11 example plugin";m.def("multiply", &py_multiply, "Convert all entries of an 1-D NumPy-array to int and multiply by 10");
}

pybind传输list相关推荐

  1. Linux系统管理必备知识之利用ssh传输文件

    在使用SSH时候,有时我们需要传输文件,这就需要用到命令scp. 从服务器上下载文件 scp username@servername:/path/filename /local_dir(本地目录) e ...

  2. http传输json文件_python

    https://cloud.tencent.com/developer/article/1571365 http传输图片 https://www.cnblogs.com/jruing/p/122156 ...

  3. python命令之m参数 局域网传输

    在命令行中使用python时,python支持在其后面添加可选参数. python命令的可选参数有很多,例如:使用可选参数h可以查询python的帮助信息: 可选参数m 下面我们来说说python命令 ...

  4. 王道考研 计算机网络笔记 第五章:传输层

    本文基于2019 王道考研 计算机网络: 2019 王道考研 计算机网络 个人笔记总结 第一章:王道考研 计算机网络笔记 第一章:概述&计算机网络体系结构 第二章:王道考研 计算机网络笔记 第 ...

  5. 光学传输与摄像头光学技术

    光学传输与摄像头光学技术 时域全反射和波导 麦克斯伟方程在时间和空间具有一定的对偶性(duality),比如空间上高斯光束的衍射与时间上高斯脉冲在具有负群速度色散的光纤中传输就具有这样的关系.科学家们 ...

  6. Thrift协议与传输选择

    1 协议 Thrift 可以让用户选择客户端与服务端之间传输通信的消息协议类别,如我们前面所讲总体划分为文本 (text) 和二进制 (binary) ,为节约带宽,提高传输效率,一般情况下使用二进制 ...

  7. 如何 SQL Server 2005 实例之间传输登录和密码

    INTRODUCTION 本文介绍如何不同服务器上的 Microsoft SQL Server 2005 实例之间传输登录和密码. 本文, 服务器 A 和服务器 B 是不同的服务器. 此外, 服务器 ...

  8. can是什么时候处于显性_can总线怎么传输数据格式过程分析

    一.什么是CAN协议 电子计算机网络用电子语言来说话,各电控单元必须使用和解读相同的电子语言,这种语言称"协议",汽车电脑网络常见的传输协议有数种.宝来车装用博世公司产品,数据总线 ...

  9. 后端如何发出请求_gRPC系列(三) 如何借助HTTP2实现传输

    本系列分为四大部分: gRPC系列(一) 什么是RPC? gRPC系列(二) 如何用Protobuf组织内容 gRPC系列(三) 如何借助HTTP2实现传输 gRPC系列(四) 框架如何赋能分布式系统 ...

最新文章

  1. python编程基础怎么学英语_英语不行而且从没碰过编程,如何0基础学python?
  2. leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String
  3. 职场心理:12个建议或许能改变你的一生(图)
  4. 诸多老牌数据仓库厂商当前,Snowflake如何创近12年最大IPO金额
  5. Pairwork2 总结
  6. 新AirPods渲染图曝光:采用黑白灰金四种配色
  7. 服务启动失败_将控制台程序转换为服务运行
  8. Oracle中 ORA-12704:字符集不匹配
  9. Windows 系统必备软件
  10. 微型计算机基础知识答案,计算机基础知识授课试题及答案
  11. mysql添加开机自启_初始化mysql并设置开机自动启动
  12. 关于机器人方面的sci论文_近十年机器人学科中国学者SCI十大发文期刊 - 论文投稿 - 小木虫 - 学术 科研 互动社区...
  13. c语言实现mysql通配符_Mysql的C语言接口简单实现电话本功能
  14. Cassini Data Rev manufacturer eals Likely Subsurface Ocean on Titan
  15. M OP N数值运算问题
  16. csp试题2:公共钥匙盒
  17. DiCOM文件判断原图是否被翻转(镜像)
  18. ES底层的Lucene文件解读
  19. 2019 CCF夏令营 day 2
  20. Osg文字的固定大小和固定朝向设置

热门文章

  1. Win7 64位的SSDTHOOK(1)---SSDT表的寻找
  2. Windows 2000和WindowsXP中神秘的数字签名
  3. Android开发精要3--Android中的Intent机制
  4. 无线传感器网络--分簇或者不分簇
  5. git简介及使用方法
  6. Android 通过http协议数据交互
  7. libnet apply method
  8. 462计算机连接错误,与Internet Explorer的运行时错误462
  9. BPF Tools 参考链接
  10. 留学计算机Ps模板,留学ps怎么写?出国留学ps模板