cython与指针数组 [英] cython with array of pointers

查看:164
本文介绍了cython与指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中有一个numpy.ndarrays(具有不同长度)的列表,并且需要非常快速地访问python中的numpy.ndarrays.我认为指针数组可以解决问题.我试过了:

I have a list of numpy.ndarrays (with different length) in python and need to have very fast access to those in python. I think an array of pointers would do the trick. I tried:

float_type_t* list_of_arrays[no_of_arrays]
for data_array in python_list_of_arrays:
    list_of_arrays[0] = data_array

但是cython抱怨:

But cython complains:

no_of_arrays < Not allowed in a constant expression

我尝试了几种方法来构造此变量:

I have tried several ways to constify this variable:

cdef extern from *:
    ctypedef int const_int "const int"

(尝试了更多创造性的尝试)-但是不幸的是,它不起作用.

(there have been more creative attempts) - however it unfortunatley doesn't work.

请帮助.

推荐答案

为什么不使用numpy对象数组而不是数组列表?

Why don't you use a numpy object array instead of a list of arrays?

我认为您遇到的问题是因为您要在堆栈中声明list_of_arrays,并且必须在编译时知道其大小.您可以尝试一些动态分配,例如:

I think the problem you're having is because you are declaring list_of_arrays in the stack, and its size must be known at compile-time. You can try some dynamic allocation, like this:

from libc.stdlib cimport malloc, free

cdef float_type_t *list_of_arrays = \
    <float_type_t *>malloc(no_of_arrays * sizeof(float_type_t*))

for i in range(no_of_arrays):
    list_of_arrays[i] = &(data_array[i].data)

# don't forget to free list_of_arrays!

(假设data_array是一个numpy数组.)

(This assumes data_array is a numpy array.)

但是,这仍在猜测您要做什么.

But this is still guessing a bit what you want to do.

这篇关于cython与指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆