Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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

TypeError: object of type 'float' has no 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
like image 790
user3042418 Avatar asked Nov 27 '13 15:11

user3042418


2 Answers

The error is exactly what is says it is. rotlati is a float. You cannot take the len() of a float. Looking at your code, it looks as if you might have meant to create lists called rotlati and rotlongi and 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.

like image 186
jez Avatar answered Oct 07 '22 10:10

jez


The len argument may be a sequence (string, tuple or list) or a mapping (dictionary). 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

like image 4
tremendows Avatar answered Oct 07 '22 11:10

tremendows