16

I want to transform the below python code in Cython:

x_array = [] x_array.append(x_new) 

I tried the following Cython codes but it gives error:

cdef np.ndarray[double, dim=1] x_array x_array.append(x_new) 

The error shows:

Cannot coerce list to type [double, dim=1]

3
  • Welcome to stack overflow. When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. Please have a read on how to provide a Minimal, Complete, and Verifiable example. Commented May 4, 2018 at 9:52
  • 2
    You've typed is as a numpy array rather than a list. There isn't really an advantage to trying to fixing the type of builtin Python objects such as list Commented May 4, 2018 at 10:30
  • is there a way to fix the problem? i have a lot of list objects in my python code. Is there a way of using Cython to improve the performance? Commented May 7, 2018 at 3:30

1 Answer 1

21

Your options are:

  1. cdef list x_array. This lets Cython know that the type of x_array is actually a list. You may get a small speed-up from this.

  2. Make x_array a numpy array instead. If all the elements in the list are the same simple, numeric type then this is probably a better option. Be aware that appending to numpy arrays is likely to be pretty slow, so you should calculate the size in advance.

    cdef np.array[double, dim=1] x_array = np.zeros((some_precomputed_size,)) # or cdef double[:] x_array = np.zeros((some_precomputed_size,)) 

    Note that this will only give you a speed-up for some types of operations (mostly accessing individual elements in Cython)

  3. If you're set on using Python lists you can sometimes get a speed-up by accessing them through the Python C API in Cython. This answer provides an example of where that worked well. This works best when you know a size in advance and so you can pre-allocate the array (i.e. don't append!) and also avoid some Cython reference counting. It's very easy to go wrong and make reference counting errors with this method, so proceed carefully.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.