Python ctypes - TypeError:int expected而不是float

时间:2014-08-27 01:00:33

标签: python python-3.x ctypes

我尝试将python列表转换为ctype数组。但不知怎的,我总是得到错误" TypeError:int expect而不是float"与此代码一致:

    self.cValues = (ctypes.c_int * len(self.values))(*self.values)

我之前创建过几行self.values。它是一个普通的python列表。 我100%肯定,len(self.values)是一个整数,我用type()函数测试它,结果是class' int'。那么这条线还有什么问题呢?谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

问题不在于len()函数,而是self.values是浮点数列表而不是整数列表。尝试使用一个小的自包含示例来查看:

import ctypes
values = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
cValues = (ctypes.c_int * len(values))(*values)

使用" TypeError:int预期而不是浮动"你得到的消息。如果您将值更改为整数,即values = [0, 1, 2, 3, 4, 5]它可以正常工作。或者,既然你在上一篇评论中说self.values是一个浮动列表,你可能想要像ctypes.c_float这样使用:

values = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
cValues = (ctypes.c_float * len(values))