TypeError:' float'对象没有属性' __ getitem __',python

时间:2016-10-31 11:13:02

标签: python

我该如何解决这个问题?

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__'

2 个答案:

答案 0 :(得分:7)

__getitem__是一个特殊的python函数,它等同于运算符[]或索引或&#34; get item&#34;操作

所以,错误基本上是说有一个变量是浮点数。对于这个变量,你可以调用__getitem__函数 - 可能是通过对它进行索引操作。

基于显示行dot_B_x = pulp.lpSum([B[i][j] * x[j] for j in range(n)])作为罪魁祸首的追溯,似乎B,B [i]或x似乎是可能的问题

答案 1 :(得分:4)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://www.worldinbag.com/js/amazingslider.js"></script> <script src="http://www.worldinbag.com/js/initslider-1.js"></script> <div id="amazingslider-wrapper-1"> <div id="amazingslider-1"> <ul class="amazingslider-slides" style="display:none;"> <li> <img src="http://uupload.ir/files/4hlh_img-10.jpg" alt="img-10" title="<a href='#'>This is a link</a> This is a caption This is a caption This is a caption This is a caption " data-description="This is a caption This is a caption This is a caption This is a caption This is a caption " /> </li> <li> <img src="http://uupload.ir/files/8dn2_img-11.jpg" alt="img-10" title="img-10" data-description="<a href='#'>This is a link</a>" /> </li> </ul> <ul class="amazingslider-thumbnails" style="display:none;"> <li> <img src="http://uupload.ir/files/zck1_img-10-tn.jpg" alt="img-10" title="img-10" /> </li> <li> <img src="http://uupload.ir/files/jcyq_img-11-tn.jpg" alt="img-11" title="img-11" /> </li> </ul> </div> </div>

pulp.lpSum([B[i][j] * x[j] for j in range(n)])

这意味着TypeError: 'float' object has no attribute '__getitem__'BB[i]都是花车,而您无法使用x