类型错误:'float' 对象没有属性 '__getitem__',python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40341026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: 'float' object has no attribute '__getitem__',python
提问by Evanildo Rocha
How do I fix this?
我该如何解决?
Traceback (most recent call last):
File "l3.py", line 45, in <module>
z = solve_minmax(n, a, B, x_min=-1000, x_max=1000)
File "l3.py", line 33, in solve_minmax
dot_B_x = pulp.lpSum([B[i][j] * x[j] for j in range(n)])
TypeError: 'float' object has no attribute '__getitem__'
回答by AbdealiJK
The __getitem__
is a special python function which is equivalent to the operator []
or the indexing or the "get item" operator.
这__getitem__
是一个特殊的 python 函数,相当于运算符[]
或索引或“获取项目”运算符。
So, the error is basically saying that there is a variable which is a float. And to this variable you've called the __getitem__
function - probably by doing an index operator to it.
所以,错误基本上是说有一个变量是浮点数。对于这个变量,你已经调用了__getitem__
函数——可能是通过对它执行索引运算符。
Based on the traceback which shows the line dot_B_x = pulp.lpSum([B[i][j] * x[j] for j in range(n)])
as the culprit, it seems either B, B[i], or x would be the probable issue
根据显示该行为dot_B_x = pulp.lpSum([B[i][j] * x[j] for j in range(n)])
罪魁祸首的回溯,似乎 B、B[i] 或 x 可能是问题
回答by DeepSpace
pulp.lpSum([B[i][j] * x[j] for j in range(n)])
pulp.lpSum([B[i][j] * x[j] for j in range(n)])
TypeError: 'float' object has no attribute '__getitem__'
TypeError: 'float' object has no attribute '__getitem__'
Which means that either B
, B[i]
or x
are floats, and you can't use []
on these.
这意味着B
,B[i]
或x
是浮点数,您不能[]
在这些上使用。