python - 在 Cython 中将 Python 列表转换为 Vector<int>

标签 python cython

我需要在 cdef 函数中将 Python 整数列表转换为 vector[int] 以调用另一个 C 函数。我试过这个:

cdef pylist_to_handles(hs):
    cdef vector[int] o_vect
    for h in hs:
        o_vect.push_back(h)
    return o_vect

这应该可以工作,因为我只需要从其他 cdef 函数调用它,但我得到了这个错误:

Cannot convert 'vector<int>' to Python object

我做错了什么?

最佳答案

在 Cython 0.17 中使用 libcpp.vector ,你可以这样做:

cdef vector[int] vect = hs

关于python - 在 Cython 中将 Python 列表转换为 Vector<int>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11454476/

相关文章:

python - 从 2 列创建一个表

python - reshape 字典键

python-3.x - 如何在 Cythonize() 上使用 annotate=True

python - 是否可以在 cython 中访问 Polars 的底层数据?

python - 字符串中的相邻字母

python - 跨服务器同步文件名

python - Pyspark UDF获取openCV问题描述符的问题

python - 在 buildozer 中编译 Cython 文件时出错

python - 这个 import_umath 函数是什么?

ipython - Ipython 中的 Cython : ERROR: Cell magic `%%cython` not found