How do I convert a Python list of lists of lists into a C array by using ctypes?

Posted by pc05 on Stack Overflow See other posts from Stack Overflow or by pc05
Published on 2013-10-17T18:05:38Z Indexed on 2013/10/17 21:54 UTC
Read the original article Hit count: 168

Filed under:
|
|
|
|

As seen here How do I convert a Python list into a C array by using ctypes? this code will take a python array and transform it to a C array.

import ctypes
arr = (ctypes.c_int * len(pyarr))(*pyarr)

Which would the way of doing the same with a list of lists or a lists of lists of lists?

For example, for the following variable

list3d = [[[40.0, 1.2, 6.0, 0.3], [50.0, 4.2, 0, 0]], [[40.0, 1.2, 6.0, 0.3], [50.0, 4.2, 0, 0]], [[40.0, 1.2, 6.0, 0.3], [50.0, 4.2, 0, 0]]]

I have tried the following with no luck:

([[ctypes.c_double * 4] *2]*3)(*list3d)
# *** TypeError: 'list' object is not callable

(ctypes.c_double * 4 *2 *3)(*list3d)
# *** TypeError: expected c_double_Array_4_Array_2 instance, got list

Thank you!

EDIT: Just to clarify, I am trying to get one object that contains the whole multidimensional array, not a list of objects. This object's reference will be an input to a C DLL that expects a 3D array.

© Stack Overflow or respective owner

Related posts about python

Related posts about c