简体   繁体   English

将 c char numpy 数组快速转换为 python 字符串列表

[英]Fast conversion of c char numpy array to list of python strings

I'm making an interface between Python and Fortran code with Cython.我正在使用 Cython 在 Python 和 Fortran 代码之间建立接口。 One part of that is retrieving arrays of strings.其中一部分是检索字符串数组。 In Fortran,在 Fortran 中,

character(len=3) :: str_array(:)

For the sake of this example, suppose str_array contains the following为了这个例子,假设str_array包含以下内容

allocate(str_array(2))
str_array = ['abc','def']

My approach is to return this to Cython as a single C char array.我的方法是将其作为单个 C 字符数组返回给 Cython。 I end up with a numpy array of byte strings:我最终得到了一个 numpy 字节字符串数组:

c_str_arr = np.array([b'a', b'b', b'c', b'd', b'e', b'f'], dtype='|S1')

I then convert this numpy array to a list of python strings with the following python code:然后,我使用以下 python 代码将此 numpy 数组转换为 python 字符串列表:

str_len = 3
arr_len = 2
c_str_arr.shape = (arr_len,str_len)
str_arr = []
for i in range(arr_len):
    str_arr.append(b''.join(c_str_arr[i]).decode())

But this is pretty slow.但这很慢。

My question: Is there a faster way to convert c_str_arr to a list of python strings?我的问题:是否有更快的方法将c_str_arr转换为 python 字符串列表?

Basically, avoid iteration over the array.基本上,避免对数组进行迭代。 This is a bit of a shot in the dark, but try:这有点像在黑暗中拍摄,但请尝试:

bs = c_str_arr.tobytes()
str_arr = [bs[i:i+str_len].decode() for i in range(0, str_len*arr_len, str_len)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM