类型错误:'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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:25:00  来源:igfitidea点击:

TypeError: 'float' object has no attribute '__getitem__',python

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 xare floats, and you can't use []on these.

这意味着B,B[i]x是浮点数,您不能[]在这些上使用。