类型错误:"float"对象不能解释为索引,如何解决?



错误消息为:

File "dry.py", line 1184, in < module >
grid = getmap(grid)
File "dry.py", line 690, in getmap
data = array(data, 'f').reshape(pts[2], pts[1], pts[0])
TypeError: 'float' object cannot be interpreted as an index

我将不胜感激任何帮助。

你能在你的问题中写一个例子来说明pts[0],pts[1],pts[2]的值是什么样子的吗?

如果值接近整数(如 1.00004 或 3.9998(,则可以使用 round 函数将值舍入到最接近的整数,如下所示:

data = array(data, 'f').reshape(round(pts[2]), round(pts[1]), round(pts[0]))

函数numpy.reshape接受整数的int或元组。您正在尝试通过float.但你不能。

顺便说一下,你可以像int(pts[0])一样将浮点对象转换为 int

检查pts[i]的类型(对于[0,1,2]中的i(

这些是int还是float? 此 API 需要int

如果您发现浮点数非常接近整数(如3.02.99(,那么显然ints 是有意的。

只需使用int(pts[i])而不是pts[i](和/或检查这些索引是如何转换为float的(。

如果你发现不打算int的值,比如2.4,那么你将需要调试它。

相关内容

最新更新