使用这样的for循环:
for k in time :
def byPrice(stock):
st = stock.get_momentum
return st[k]
s = sorted(obj, key=byPrice)
我想对stock
对象列表按每个stock对象中动量数组中的第k项进行排序
class stock:
def __init__(self, name, price):
self.name = name
self.lens = len(price)
def get_momentum(self):
momentum = []
for i in np.arange(lens):
count = close[i]/close[i-60]
momentum.append(count)
return momentum
但是我收到一条警告说'method' object is not subscriptable
。错误在st[k]
行
只需添加括号:
你需要实际调用你的方法。否则,
st
是方法对象,st[k]
没有意义您也可以在不使用定义
byPrice
的情况下执行此操作(不过,可以说更难阅读)。或者可以在循环外定义
byPrice
,并让它将k
作为另一个参数您正在将变量
st
设置为实际的类方法stock.get_momentum
,而方法/函数对象没有基于索引的访问权限。这就是not subscriptable
的意思只是一个小错误,经常发生!将
st = stock.get_momentum
更改为st = stock.get_momentum()
相关问题 更多 >
编程相关推荐