7

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

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.

Please help.

1 Answer 1

5

Why don't you use a numpy object array instead of a 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!

(This assumes data_array is a numpy array.)

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Tiago, it's critical for me that I don't have any python overhead when accessing each of those arrays individually. Currently they reside in a list and I often need to switch between them and that is an amazing time drain.
So I tried out the object nparray - that's the way to go. cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.