繁体   English   中英

TypeError:范围索引必须是整数或切片,而不是元组

[英]TypeError: range indices must be integers or slices, not tuple

我的代码中有两个嵌套的 for 循环,但一个有效,另一个无效,它们也是相同的,它在我的脑海中

此代码有效

def nearest(furthestpoints,lastpoints,lastIDs,currentpoints,img,threshold = 30):
    currentIDs = np.empty(0,dtype = float)
    normList = np.empty(0,dtype = float)

    for i in range(0,len(currentpoints)):
        for a in range(0,len(lastpoints)):
            normList = np.append(normList,np.linalg.norm(currentpoints[i,:] - lastpoints[a,:]))```

但是这段代码会产生错误

File "/home/oliver/tracker.py", line 22, in getLostPoints
    normList = np.append(normList,np.linalg.norm(current[i,:] - last[a,:]))
TypeError: range indices must be integers or slices, not tuple
def getLostPoints(last,lastIDs,current,threshold = 50):

    normList = np.empty(0,dtype = float)
    goodIDs = np.empty(0,dtype = int)
    lostPoints = np.empty((2,0),dtype = float)

    for i in range(0,len(last)):
        for a in range(0,len(current)):
            normList = np.append(normList,np.linalg.norm(current[i,:] - last[a,:]))

        if min(normList) < threshold:
            indexMin = np.argmin(normList)
            goodIDs = np.append(goodIDs,lastIDs[indexMin])
        normList = np.empty(0,dtype = float) 

这些只是循环,这段代码还有更多内容

输入 arrays 的形状为(n,2)其中n是点数

我没有足够的声誉来发表评论,所以我必须发布这个作为答案。

首先,你有:

    for i in range(0,len(current)):
        for a in range(0,len(current)):

你的意思:

    for i in range(0,len(current)):
        for a in range(0,len(last)):

这将遵循与您的有效方法相同的模式。

您是否尝试过在第二种方法中打印ia作为完整性检查以及打印输入的特征(形状等)? 这不是最 eloquent 的调试方式,但打印值只是为了确保它们是您所期望的,这可能是有见地的。

发现问题这是一个简单的错误,当我在主循环中调用代码时它现在可以正常工作了感谢您的回答

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM