How do I convert a Python list of lists of lists into a C array by using ctypes?
NickName:pc05 Ask DateTime:2013-10-18T02:05:38

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

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.

Copyright Notice:Content Author:「pc05」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/19434026/how-do-i-convert-a-python-list-of-lists-of-lists-into-a-c-array-by-using-ctypes

Answers
Eryk Sun 2013-10-17T21:58:48

It works with tuples if you don't mind doing a bit of conversion first:\n\nfrom ctypes import *\n\nlist3d = [\n [[0.0, 1.0, 2.0, 3.0], [4.0, 5.0, 6.0, 7.0]], \n [[0.2, 1.2, 2.2, 3.2], [4.2, 5.2, 6.2, 7.2]],\n [[0.4, 1.4, 2.4, 3.4], [4.4, 5.4, 6.4, 7.4]],\n]\n\narr = (c_double * 4 * 2 * 3)(*(tuple(tuple(j) for j in i) for i in list3d))\n\n\nCheck that it's initialized correctly in row-major order:\n\n>>> (c_double * 24).from_buffer(arr)[:]\n[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, \n 0.2, 1.2, 2.2, 3.2, 4.2, 5.2, 6.2, 7.2, \n 0.4, 1.4, 2.4, 3.4, 4.4, 5.4, 6.4, 7.4]\n\n\nOr you can create an empty array and initialize it using a loop. enumerate over the rows and columns of the list and assign the data to a slice:\n\narr = (c_double * 4 * 2 * 3)()\n\nfor i, row in enumerate(list3d):\n for j, col in enumerate(row):\n arr[i][j][:] = col\n",


More about “How do I convert a Python list of lists of lists into a C array by using ctypes?” related questions

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

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)

Show Detail

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

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)

Show Detail

Python lists and ctypes

Hi I am trying to call a c++ function that has the following signature change_mountain_heights(mnt *mountains[],float32 heights[],int8 num_elements) The idea being from python I can have a list of

Show Detail

How to get ctypes type object from an ctypes array

Actually, I'm trying to convert ctypes arrays to python lists and back. If found this thread. But it assumes that we know the type at compile time. But is it possible to retrieve a ctypes type fo...

Show Detail

How to convert a list of lists to an array of lists?

I have a list of lists in R. I would like to convert it to an array of lists, but I only get an array of lists of lists: r <- list(list(a=1, b=NULL, c=NULL, d=1.23), list(a=2, b=NULL, c=

Show Detail

Faster way to convert ctypes array to python list?

I was wondering if someone could advise me whether there is a better/faster approach to read data from my C program that outputs two lists of size n. I am using ctypes to call the C program. The l...

Show Detail

Convert a flat list to list of lists in python

One may want to do the contrary of flattening a list of lists, like here: I was wondering how you can convert a flat list into a list of lists. In numpy you could do something like: >>> a=

Show Detail

How to pass lists into a ctypes function on python

This is a 2 part question. To give some background, I have a C code as follows: int c_func(const char* dir, float a, float b, float c, float d ) { printf("%s\n", dir); printf("%f\n",a);

Show Detail

Ctypes: fast way to convert a return pointer to an array or Python list

I am using ctypes to pass an array pointer to a dll and return a pointer to an array of doubles that was created using malloc in the dll. On return to Python, I need a fast way to convert the point...

Show Detail

how to convert list of strings into list of lists using python

I have a list of strings as shown below: value=['[a,b,c]', '[d,e,f]'] I want to convert it into list of lists and assign it to a variable **needed output** value=[['a','b','c'], ['

Show Detail