2015-04-16 27 views
0

我有一种方法,我从SQLite数据库检索数据。数据保存为文本(出于某种原因),但它确实是浮点数(14.5等)。这是我的方法,让他们:我的类型TypeError发生了什么? (Python)

def retrieveSpeeds(databasepath, someid): 
    con = lite.connect(databasepath) 
    with con: 
     cur = con.execute("SELECT speed FROM speeds WHERE id = someid") 
     speeds = [x[0] for x in cur] 
     return speeds 

这将返回以下:

[u'14.00', u'14.50', u'14.50', u'14.50', u'14.50', u'13.80'] 

然而,因为我想简单的数字我做的:

for i in range(0, len(speed)): 
    newspeeds.append(float(speed[i])) 

所以现在新的返回外观像:

[14.0, 14.5, 14.5, 14.5, 14.5, 13.8] 

所以在我的主要我做的:

maxspeeds = [] 
for id in userid: 
    speed = retrieveSpeeds(databasepath, id) 
    if len(speed>0): 
      maxspeeds.append(max(speed)) 
for i in range(0,len(maxspeeds)): 
    if maxspeeds[i] > 40: 
      maxspeeds = maxspeeds.pop(i) 

这给了我以下类型错误:

Traceback (most recent call last): 
    if len(speed>0): 
    TypeError: object of type 'bool' has no len() 

布尔?我非常困惑,为什么我的返回列表现在是一个布尔。

+2

你不应该使用'range'和'len'进行迭代。就像你最初使用列表理解一样:'velocity = [float(x [0])for x in cur]' –

回答

4

该错误消息说,你写if len(speed>0):(注意len>0),而不是len(speed)>0,你应该写它(和你在你的代码片段所做的那样)。

1

看,你有一个错字,正确的方法是做if len(speed)>0:。你自己纠正了它。

len(5>2)将无法​​正常工作无论是作为5>2回报TrueTrue没有len()