还有一个“instance没有属性''uugetitem\''”cas

2024-06-16 10:14:54 发布

您现在位置:Python中文网/ 问答频道 /正文

这似乎在堆栈溢出时被打得死去活来,但似乎没有一个问题符合我的问题。总之,直奔代码。在

这是边.py在

from __future__ import division
import sys
from numpy import *


class EdgeList:
    def __init__(self, mat):
        self.id1 = mat[:,0]
        self.id2 = mat[:,1]
        self.value = mat[:,2]
    def is_above(self):
        return self.value>average(self.value)
    def stats(self):
        pass #omitted; too long and irrelevant here.

这是啊稀疏.py在

^{pr2}$

这是一些测试代码(其他测试通过)

import sys
from numpy import *
from Edge import EdgeList
from AHsparse import AHvector

testmat =loadtxt('test.data', delimiter=';')
st = EdgeList(testmat)
stv = AHvector(st)
stv = stv.multiply(stv)
print(stv)

错误发生在AHvector类的init方法,但是调用边.py公司名称:

Traceback (most recent call last):
  File "/Users/syntaxfree/Dropbox/nina/nina lives in objects/sparse_test.py", line 8, in <module>
    stv = AHvector(st)
  File "/Users/syntaxfree/Dropbox/nina/nina lives in objects/AHsparse.py", line 9, in __init__
    self.el = EdgeList(mat)
  File "/Users/syntaxfree/Dropbox/nina/nina lives in objects/Edge.py", line 7, in __init__
    self.id1 = mat[:,0]
AttributeError: EdgeList instance has no attribute '__getitem__'
[Finished in 0.6s with exit code 1]

恐怕我没有其他要添加的东西了——除了我能够自己初始化EdgeList并在其他测试代码中运行stats方法之外,我完全搞不懂为什么这样做行不通。在


Tags: infrompyimportselfinitvaluedef
1条回答
网友
1楼 · 发布于 2024-06-16 10:14:54

当你跑的时候

 stv = AHvector(st)

st是边缘列表。然后,AHvector的init尝试生成st的边缘列表。也许AHvector应该说

^{pr2}$

或者也许AHvector不应该接收st,而是{}?在

相关问题 更多 >