0

I would like to know how can I create an array of structs in Cython that I can populate and make computations afterwards.

Example

Here I have the Cython code

%%cython 
cimport numpy as cnp
cimport cython


from collections import namedtuple

Couple = namedtuple('Couple', ['female', 'male'], verbose=False)

cdef struct CyCouple:
    int female
    int male


cpdef int np_cy_count_women_earning_more2(list py_couples):

    cdef:
        int count = 0, r, N
        CyCouple cy_couples[100_0000] # THIS IS HARDCODED

    N  = len(py_couples)

    make_CyCouple_array(py_couples, cy_couples, N)

    for n in range(N):
        r = cy_couples[n].female > cy_couples[n].male
        count += r
    return count

I would like to have a general versions instead of the definition in # THIS IS HARDCODED.

What could I do?

David Buchaca
  • 206
  • 1
  • 5
  • There is **a lot** of unnecessary code, if all you want to know is how to dynamically allocate cy_couples! – ead Mar 21 '19 at 19:30
  • Possible duplicate of [Cython: creating an array throws "not allowed in a constant expression"](https://stackoverflow.com/questions/46850610/cython-creating-an-array-throws-not-allowed-in-a-constant-expression) – DavidW Mar 21 '19 at 19:42

0 Answers0