Question

If I have a jagged list in python like

jagged = [[1],[2,3]]

how can I declare it as an array using numpy and cython? Is it possible somehow? I know the following syntax, but it won't do, as it gives only a 1D array.

cdef numpy.ndarray[np.float_t] jagged
Was it helpful?

Solution

Numpy arrays (both in and out of Cython) are typically rectangular. You don't say what you're trying to do with the jagged array, but you might be able to use:

http://docs.scipy.org/doc/numpy/reference/maskedarray.html

or:

http://docs.scipy.org/doc/scipy/reference/sparse.html

As for the 1-dimensional part of your question, you're wanting the ndim param (mode='c' can speed things up if you won't be getting any order='F' arrays):

cdef numpy.ndarray[DTYPE_t, ndim=2, mode='c'] somearray
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top