如何通过ctypes将列表列表(非空)从Python传递到C++

How to pass (non-empty) list of lists from Python to C++ via ctypes?

本文关键字:列表 Python C++ 非空 ctypes 何通过      更新时间:2023-10-16

我有一些格式为的数据

data = [[1,1,1],[2,2,2],[3,3,3]]

如何通过ctypes将其传递给C++?

我可以单独传递每个列表,如下所示:

import ctypes
temp1 = [1,1,1]
temp2 = [2,2,2]
temp3 = [3,3,3]
list1 = (ctypes.c_int * 3)(*temp1)     #NO IDEA WHAT THE * MEANS
list2 = (ctypes.c_int * 3)(*temp2)
list3 = (ctypes.c_int * 3)(*temp3)

但在那之后,如果我试图将所有这些列表附加到"数据"中。。。

data.append(list1)
data.append(list2)
data.append(list3)
data_final = (ctypes.?????? * 3)(*data)

我应该放什么类型的?????感谢

??????应为ctypes.c_int * 3 * 3

data_final = (ctypes.c_int * 3 * 3)(*data)
[list(a) for a in data_final]
# --> [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

为了记录,不要进行

data = []
data.append(list1)
data.append(list2)
data.append(list3)

这是python,不是c++,做

data = [list1, list2, list3]

见鬼,因为你只是想把它传给一个函数做

data_final = (ctypes.c_int * 3 * 3)(list1, list2, list3)

并且完全跳过CCD_ 3步骤


如果我在N x M列表py_list中有数据,我会进行

c_array = (c_types.c_int * M * N)(*[(c_types.c_int * M)(*lst) for lst in py_list])