加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

在Cython中将Python列表转换为Vector

发布时间:2020-12-20 12:28:43 所属栏目:Python 来源:网络整理
导读:我需要在cdef函数中将int的int列表转换为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函数调用它,但我收到此错误
我需要在cdef函数中将int的int列表转换为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

我究竟做错了什么 ?

解决方法

你真正拥有的是:

cdef object pylist_to_handles(hs):
    ...
    return <object>o_vect

如果没有明确设置类型,则假定它是一个python对象(代码中的“对象”).正如您在代码中看到的那样,您正在尝试将vector [int]转换为对象,但Cython不知道如何处理它.

只需在cdef中添加一个返回类型:

cdef vector[int] pylist_to_handles(hs):

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读