简体   繁体   中英

Pass list of list from Python to C as uint64_t matrix

How can a list of list of numbers (uint64_t in C) for example: a = [[1, 5, 9], [4, 6, 7], [8, 2, 3]] be passed as an argument of a C program (via argv) and then be printed (or be able to access that data) in the C program as a 2D matrix?

In my C program I'm working with non-predefined size matrices of unint64_t type ( uint64_t matrix[nrows][ncols] )

There's many ways to do it, but the easiest is probably just flattening the matrix first:

def flatten(a):
    yield len(a)
    cols = max(len(i) for i in a)
    yield cols
    for i in a:
        for j in i:
            yield j
        for j in range(0, cols - len(i)):
            yield 0

Then you can use the result of that to feed the parameters to your C program, and from that you can build the matrix (C99 required):

#include <stdlib.h>
#include <inttypes.h>

void load_matrix(uint64_t *matrix, char **p, int alen) {
    while (alen-- > 0)
        *matrix++ = atoi(*p++);
}

void do_work(uint64_t matrix[nrows][ncols], int nrows, int ncols) {
    ...
}

int main(int argc, char **argv) {
    int nrows = atoi(argv[1]), ncols = atoi(argv[2]);

    // check ncols and nrows and argc
    ...

    uint64_t *matrix;
    matrix = malloc(nrows * ncols * sizeof(*matrix));

    // check matrix isn't NULL
    ...

    load_matrix(matrix, argv + 3, nrows * ncols);
    do_work(matrix, nrows, ncols);
    ...
}

Keep in mind it's just an illustration, ie things like atoi aren't appropriate.

If you want to use C89 you need to cope with a flat array, in the end it's the same thing.

However, if the matrix is large, you don't really want that; you would serialize it in a similar way (perhaps with a more efficient implementation), but you would want to deal with binary data directly, and pass that through shared memory, a file, a socket, or a pipe to your C program, so there's no need for further processing.

If you were working with a very large data set, then you definitely want to work with the same representation in both Python and C, and you probably want to use shared memory or files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM