快速字符串数组 - Cython

Jen*_*das 10 python arrays string python-2.x cython

有以下假设代码:

cdef extern from "string.h":
    int strcmp(char* str1, char* str2)

def foo(list_str1, list_str2):
    cdef unsigned int i, j
    c_arr1 = ??
    c_arr2 = ??
    for i in xrange(len(list_str1)):
        for j in xrange(len(list_str2)):
            if not strcmp(c_arr1[i], c_arr2[j]):
                do some funny stuff
Run Code Online (Sandbox Code Playgroud)

有没有办法将列表转换为c数组?

我已经阅读并尝试过Cython - 将字符串列表转换为char**,但这只会引发错误.

fal*_*tru 15

请尝试以下代码.to_cstring_array以下代码中的函数是你想要的.

from libc.stdlib cimport malloc, free
from libc.string cimport strcmp
from cpython.string cimport PyString_AsString

cdef char ** to_cstring_array(list_str):
    cdef char **ret = <char **>malloc(len(list_str) * sizeof(char *))
    for i in xrange(len(list_str)):
        ret[i] = PyString_AsString(list_str[i])
    return ret

def foo(list_str1, list_str2):
    cdef unsigned int i, j
    cdef char **c_arr1 = to_cstring_array(list_str1)
    cdef char **c_arr2 = to_cstring_array(list_str2)

    for i in xrange(len(list_str1)):
        for j in xrange(len(list_str2)):
            if i != j and strcmp(c_arr1[i], c_arr2[j]) == 0:
                print i, j, list_str1[i]
    free(c_arr1)
    free(c_arr2)

foo(['hello', 'python', 'world'], ['python', 'rules'])
Run Code Online (Sandbox Code Playgroud)

  • `PyString_AsString`只是python2,所以这个解决方案不适用于python3 (3认同)

use*_*666 7

如果您使用的是 Python 3,这里是@falsetru 答案的更新(未在 Python 2 上测试)。

cdef extern from "Python.h":
    char* PyUnicode_AsUTF8(object unicode)

from libc.stdlib cimport malloc, free
from libc.string cimport strcmp

cdef char ** to_cstring_array(list_str):
    cdef char **ret = <char **>malloc(len(list_str) * sizeof(char *))
    for i in xrange(len(list_str)):
        ret[i] = PyUnicode_AsUTF8(list_str[i])
    return ret

def foo(list_str1, list_str2):
    cdef unsigned int i, j
    cdef char **c_arr1 = to_cstring_array(list_str1)
    cdef char **c_arr2 = to_cstring_array(list_str2)

    for i in range(len(list_str1)):
        for j in range(len(list_str2)):
            if i != j and strcmp(c_arr1[i], c_arr2[j]) == 0:
                print(i, j, list_str1[i])
    free(c_arr1)
    free(c_arr2)

foo(['hello', 'python', 'world'], ['python', 'rules'])
Run Code Online (Sandbox Code Playgroud)

警告:返回的指针PyUnicode_AsUTF8缓存在父 unicode 对象中。这有两个后果:

  1. 这个指针只有在父 unicode 对象还活着时才有效。之后访问它会导致未定义的行为(例如可能的分段错误)。
  2. 的调用者PyUnicode_AsUTF8 负责释放内存。