Creating a length zero cython array

2019-08-13 22:37发布

I would like to create a zero length array in Cython:

from cython.view cimport array as cvarray

arr = cvarray(shape=(0,), itemsize=sizeof(int), format="i")

However, I get this error:

ValueError: Invalid shape in axis 0: 0.

Is there a reason why I can't create a zero length array? Numpy allows zero length arrays, but I'm trying to avoid it, as I only want to use C functions in my code.


In response to hpaulj's question; here is an example use-case where I need it. The example fails when the input array arr is of length 1. I would prefer to return an empty array, instead of a None object. As I would like the return type to be consistent.

from cython.view cimport array as cvarray

cpdef delete(int[:] arr, int obj):
    cdef:
        int[:] result
        int j, i

    result = cvarray(shape=(len(arr)-1,), itemsize=sizeof(int), format="i")
    j = 0
    for i in range(result.shape[0]):
        if i==obj:
            j+=1
        result[i] = arr[j]
        j+=1
    return result

标签: python cython
0条回答
登录 后发表回答