首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python TypeError:'__getitem__‘对象没有属性’__getitem__‘

Python TypeError:'__getitem__‘对象没有属性’__getitem__‘
EN

Stack Overflow用户
提问于 2014-06-02 02:50:48
回答 1查看 7.9K关注 0票数 0

有人知道怎么解决这个问题吗?我现在正在学习集体智慧,当我编写这个代码时,我把它和另一个例子进行了比较。但它会犯这样的错误:

代码语言:javascript
复制
Traceback (most recent call last):   File "<pyshell#8>", line 1, in
 <module>
    clust=clusters.hcluster(data)   File "D:\Kuliah\smt1\Phyton Class\contoh coding\coding-collective
intelligence\myself_Maulida\bab3-documentClustering\clusters.py", line
78, in hcluster
    for i in range(len(clust[0].vec))] TypeError: 'float' object has no attribute '__getitem__'

这是我的密码,有人能帮忙吗?谢谢。

代码语言:javascript
复制
def hcluster(rows,distance=pearson):
    distances={}
    currentclustid=-1

    #clusters are initially just the rows
    clust=[bicluster(rows[i],id=i) for i in range(len(rows))]

    while len(clust)>1:
        lowestpair=(0.1)
        closest=distance(clust[0].vec,clust[1].vec)

        #loop through every pair looking for the smallest distance 
        for i in range(len(clust)):
            for j in range(i+1,len(clust)):
            #distance is the cache of distance calculations
                if(clust[i].id,clust[j].id) not in distances:
                    distances[(clust[i].id,clust[j].id)]=distance(clust[i].vec,clust[j].vec)

                    d=distances[(clust[i].id,clust[j].id)]

                if d<closest:
                    closest=d
                    lowestpair=(i,j)

            #calculate the average of the two cluster
            mergevec=[
            (clust[lowestpair[0]].vec[i]+clust[lowestpair[1]].vec[i])/2.0 
            for i in range(len(clust[0].vec))]

            #create the new cluster
            newcluster=bicluster(mergevec,left=clust[lowestpair[0]],
                                 right=clust[lowestpair[1]],
                                 distance=closest,id=currentclustid)

            #cluster ids that weren't in the original set are negative
            currentclustid-=1
            del clust[lowestpair[1]]
            del clust[lowestpair[0]]
            clust.append(newcluster)

        return clust[0]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-02 02:55:14

代码语言:javascript
复制
lowestpair=(0.1)

那是句号,不是逗号。lowestpair是一个浮点数,而不是元组。(虽然这个错误看起来来自clust[0],但是Python并不擅长指出一个逻辑行的哪一行是错误的。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23986701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档