C ++矢量到Python 3.3 [英] C++ vector to Python 3.3

查看:102
本文介绍了C ++矢量到Python 3.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从C ++脚本中获取python列表,例如 [1,2,3,4] 。我写了C ++脚本,该脚本返回一个向量。

I would like to get a python list, say, [1,2,3,4], from a C++ script. I wrote the C++ script, which returns a vector.

如何在不使用SWIG / SIP / Cython /及其它的情况下连接两端?

How to connect the ends without SWIG/SIP/Cython/and others?

仅将C ++编译为.exe或elf文件,然后从命令行调用会更容易吗?让.exe创建一个包含矢量的.txt并使用python读取它?

Could it be easier to just compile the C++ to an .exe or elf file and then call from command line, have the .exe create a .txt containing a vector and read it in with python?

我的观点是,我只需要一个C ++很小的函数就可以对海量数据进行繁重的计算。做到这一点最痛苦,最短的方法是什么?

My point is, I only need a really small function from C++ to do the heavy calculations on huge data. What would be the least painful and shortest method to do just this?

编辑:
举一个例子。 Python将给C ++一个文件名字符串( foo.txt),然后它将读取文件的上下文(200,000行乘300列),计算丢失的数量,然后将每行的丢失数量返回给Python。这将产生200,000个数字的列表。
两者之间如何进行这种交流?

To give an example. Python will give a filename string to C++ ("foo.txt"), which will then read the context of the file (200,000 rows by 300 columns), count the missings and then return to Python the amount of missings per row. This yields a list of 200,000 numbers. How to have this communication between both of them?

出于完整性考虑,这就是我仍然想知道的解决方法:

Just for completeness, this is what I am still wondering about how to go about:


  • 将python文件名字符串传递给C ++

  • 在C ++中接收python字符串

  • DONE在C ++中创建矢量

  • 将向量返回到Python

  • 在Python中接收向量

  • Pass python filename string to C++
  • Receive python string in C++
  • DONE Create vector in C++
  • Return vector to Python
  • Receive vector in Python

推荐答案

现在可能尚无定论,我在您的其他问题,但是我已经针对Python 3.3和C ++而不是Python 2.7和C修改了该版本。

This is probably moot now, and I posted something similar on your other question, but I've adapted this version for Python 3.3 and C++ rather than Python 2.7 and C.

返回一个Python列表对象,并且由于您正在构建一个可能很长的列表(200,000个项目),因此在C ++代码中构建Python列表可能比构建 std :: vector ,然后稍后将其转换为Python列表n。

If you want to get back a Python list object, and since you're building a list which could potentially be very long (200,000 items), it's probably more efficient to build the Python list in the C++ code, rather than building a std::vector and then converting that to a Python list later on.

根据您另一个问题中的代码,我建议使用类似这样的内容...

Based on the code in your other question, I'd suggest using something like this...

// foo.cpp
#include <python3.3/Python.h>
#include <fstream>
#include <string>
using namespace std;

extern "C"
{
    PyObject* foo(const char* FILE_NAME)
    {
        string line;
        ifstream myfile(FILE_NAME);
        PyObject* result = PyList_New(0);

        while (getline(myfile, line))
        {
            PyList_Append(result, PyLong_FromLong(1));
        }

        return result;
    }
}

...与...编译>

...compiled with...

$ g++ -fPIC -shared -o foo.so foo.cpp -lpython3.3m

...以及用法示例...

...and an example of usage...

>>> from ctypes import *
>>> foo = CDLL('./foo.so')
>>> foo.foo.restype = py_object
>>> foo.foo(b'foo.cpp')
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

...尽管您需要转换现有的 std :: vector 到Python列表,您可以通过将向量的长度传递到 PyList_New() ,然后使用 PyList_SetItem() 代替 PyList_Append()

...although if you need to convert an existing std::vector to a Python list, you can pre-allocate the memory needed by the Python list by passing the length of the vector into PyList_New(), and then use PyList_SetItem() instead of PyList_Append().

我能想到的唯一其他方法是...

The only other methods I can think of would be...


  1. 要在Python中预分配一个RAM块,并让C ++函数填充值,就像qarma的回答一样,但是您必须提前知道多少RAM分配。您可以选择一个任意值,但是鉴于文件中的行数不是事先知道的,因此该数目可能太大或太小。

  1. To pre-allocate a block of RAM in Python, and have the C++ function fill in the values, like in qarma's answer, but you'd have to know in advance how much RAM to allocate. You could just pick an arbitrary value, but given that the number of lines in the file isn't known in advance, this number may be way too large or way too small.

要在C ++中堆分配 std :: vector ,并返回指向第一个元素和元素数量的指针,但是您必须编写完第二个函数以释放RAM。

To heap-allocate the std::vector in C++, and return a pointer to the first element, and the number of elements, but you'd have to write a second function to free the RAM once you were done with it.

无论哪种方式,您仍然可以将'returned'数组转换为Python列表的开销,所以您最好自己做。

Either way, you still have the overhead of converting the 'returned' array into a Python list, so you may as well do it yourself.

这篇关于C ++矢量到Python 3.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆