Python 类型错误:“float”类型的对象没有 len()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20246722/
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-18 19:59:45  来源:igfitidea点击:

TypeError: object of type 'float' has no len()

python

提问by user3042418

Here's the error: for j in range (len(rotlati)):

这是错误:对于范围内的 j (len(rotlati)):

TypeError: object of type 'float' has no len()

类型错误:“float”类型的对象没有 len()

I have seen many other posts but I haven't found the solution yet. I'm a bit confused. Please comment if you know what's going on here.

我看过很多其他帖子,但我还没有找到解决方案。我有点困惑。如果您知道这里发生了什么,请发表评论。

The code is:

代码是:

m = 22
rlati = numpy.zeros(m)
n = 22
rlongi = numpy.zeros(n)

v = numpy.ndarray((2,),float)

for j in range (len(lati)):
LA = lati[j]
    rlati[j] = LA - latiref

    for i in range (len(longi)):
        LO = longi[i]

    rlongi[i] = LO - longiref

    v[0] = rlati[j]
    v[1] = rlongi[i]

    vv = numpy.matrix(v)
    #transpose of vv as vv.T
    vv = vv.T

    #proper rotation
    vn = R*vv

    #define how many decimals
    vn = numpy.around(vn, decimals =2)

    # rotation of the second column (lati) and third line (longi)
        rotlati = float(vn[0])
    rotlongi = float(vn[1])

s = 22
latidef = numpy.zeros(s)
p = 22
longidef = numpy.zeros(p)

for j in range (len(rotlati)):
RLA = rotlati[j]

    latidef[j] = RLA + latiref

for i in range (len(rotlongi)):
        RLO = rotlongi[i]

    longidef[i]= RLO + longiref

    RLADEF = latidef[j]
    RLODEF = longidef[i]

    return RLADEF, RLODEF

回答by jez

The error is exactly what is says it is. rotlatiis a float. You cannot take the len()of a float. Looking at your code, it looks as if you might have meantto create listscalled rotlatiand rotlongiand append to them on each iteration of your range(len(lati))loop. Instead you're currently just overwriting the same two floating-point variables on every iteration.

错误正是所说的那样。rotlati是一个float。您不能使用len()a 的float。查看您的代码,您似乎打算在循环的每次迭代中创建名为and 的列表并附加到它们。相反,您目前只是在每次迭代时覆盖相同的两个浮点变量。rotlatirotlongirange(len(lati))

回答by tremendows

The len argument may be a sequence (string, tuple or list) or a mapping (dictionary). https://docs.python.org/2/library/functions.html#len

len 参数可以是序列(字符串、元组或列表)或映射(字典)。 https://docs.python.org/2/library/functions.html#len

Before calling the len function, you should verify if the argument is one of this type. You can call the method isinstance() to verify it. Take a look on how to use it. https://docs.python.org/2/library/functions.html#isinstance

在调用 len 函数之前,您应该验证参数是否是这种类型之一。您可以调用方法 isinstance() 来验证它。看看如何使用它。 https://docs.python.org/2/library/functions.html#isinstance

回答by Shuai

I got the correct answer, try this:

我得到了正确的答案,试试这个:

lloyd = {
    "name": "Lloyd",
    "homework": [90.0, 97.0, 75.0, 92.0],
    "quizzes": [88.0, 40.0, 94.0],
    "tests": [75.0, 90.0]
}

alice = {

    "name": "Alice",
    "homework": [100.0, 92.0, 98.0, 100.0],
    "quizzes": [82.0, 83.0, 91.0],
    "tests": [89.0, 97.0]
}
tyler = {

    "name": "Tyler",
    "homework": [0.0, 87.0, 75.0, 22.0],
    "quizzes": [0.0, 75.0, 78.0],
    "tests": [100.0, 100.0]
}

numbers = []

def average(numbers):

    total = float(sum(numbers))
    return total / len(numbers)

回答by gaurav Pawar

Another scenario i got the same error, while saving file.

另一种情况,我在保存文件时遇到了同样的错误。

odf.to_excel(writer,sheet_name=str(x),startcol=5,index=False)

odf.to_excel(writer,sheet_name=str(x),startcol=5,index=False)

You may also check the name of the sheet is int or float. Convert that to string.

您还可以检查工作表的名称是 int 还是 float。将其转换为字符串。