gpt4 book ai didi

python - 另一个 "instance has no attribute ' __getitem_ _' "案例

转载 作者:行者123 更新时间:2023-11-30 23:40:42 24 4
gpt4 key购买 nike

这似乎已经在 Stack Overflow 上被打败了,但似乎没有一个问题与我的问题相符。无论如何,直接看代码。

这是 Edge.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.

这是AH​​sparse.py

from __future__ import division

import sys
from numpy import *
from Edge import EdgeList

class AHvector:
def __init__(self, mat):
self.el = EdgeList(mat)
def multiply(self, other):
v=zeros(max(len(self.el.val), len(other.el.val)))
for index in self.id1:
v[index] = self.el.val[index] * other.el.val[index]
return v

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

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方法处,但是回调到了Edge.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 方法,而且我完全困惑为什么这不起作用.

最佳答案

当你运行时

 stv = AHvector(st)

st 是一个 Edgelist。然后AHvector的init尝试创建一个st的Edgelist。也许 AHvector 应该说明

 self.el = mat # Instead of EdgeList(mat)?

或者也许 AHvector 不应该接收 st,而是接收 testmat

关于python - 另一个 "instance has no attribute ' __getitem_ _' "案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12460523/

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