gpt4 book ai didi

python - 为什么我会收到类型错误 : float when I try to iterate my list

转载 作者:太空宇宙 更新时间:2023-11-04 07:38:04 25 4
gpt4 key购买 nike

所以我在这里解决了我有点尴尬的错误:What is happening with my types, 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]
for i in range(0, len(speed)):
newspeeds.append(float(speed[i]))
return speeds

所以返回看起来像:

[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 maxspeeds[i]>40:
TypeError: 'float' object has no attribute '__getitem__'

怎么突然想到我的list是float对象呢?我还会犯更多错误吗?

最佳答案

您正在将 maxspeeds 分配给 maxspeeds.pop(i) 的输出,它返回一个 number,它是 popped 项目。相反,不要为输出分配任何内容:

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.pop(i) #Here

.pop返回已删除项目的索引,因此当您将列表分配给该输出时,列表成为索引(整数)。

关于python - 为什么我会收到类型错误 : float when I try to iterate my list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29681859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com