在Cython中将Python字符串列表转换为vector[PyObject],可以通过以下步骤实现:
from libcpp.vector cimport vector
from cpython cimport PyUnicodeObject, PyObject
def convert_string_list_to_vector(str_list):
cdef vector[PyObject] pyobj_vector
cdef PyUnicodeObject* pyunicode_obj
for string in str_list:
pyunicode_obj = <PyUnicodeObject*>string.encode('utf-8')
pyobj_vector.push_back(<PyObject*>pyunicode_obj)
return pyobj_vector
string_list = ['string1', 'string2', 'string3']
pyobj_vector = convert_string_list_to_vector(string_list)
这样,你就可以将Python字符串列表转换为Cython中的vector[PyObject]了。
领取专属 10元无门槛券
手把手带您无忧上云