如何在 Python 中使用 ctypes 将浮点数列表转换为 C double **

问题描述 投票:0回答:1

我正在尝试将 Python 浮点数列表转换为 C double ** ptr

list = [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
c_dbl_ptr = (ctypes.c_double * len(list) * len(list[0])) (*(tuple(tuple(sublist) for sublist in list)))

这会产生

IndexError: invalid index

此方法来自如何使用 ctypes 将列表列表的 Python 列表转换为 C 数组?但在尝试将其应用于 2d 列表时,我似乎做错了一些事情。

python ctypes
1个回答
0
投票

您的列表理解有问题。应用此方法时,您将获得 ctypes.c_double 数组的列表。这是通过迭代列表中的每个数组来完成的(由于

list
是一种方法,我已将其重命名)

import ctypes
lst = [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
x = [(ctypes.c_double* len(sublst))(*sublst) for sublst in lst]

希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.