我正在使用numpy模块来检索2d数组中最大值的位置。但是这个2d数组由MyObjects组成。现在我会收到以下错误:
TypeError:不可排序类型:int() > MyObject()
我尝试使用以下代码重写int函数:
def int(self):
return self.score
但这并没有解决我的问题。我需要将MyObjects的二维数组转换为整数的二维数组,我需要扩展Integer对象(如果在Python中可能的话),或者我可以以另一种方式重写int()函数?
[编辑] 完整的对象:
class MyObject:
def __init__(self, x, y, score, direction, match):
self.x = x
self.y = y
self.score = score
self.direction = direction
self.match = match
def __str__(self):
return str(self.score)
def int(self):
return self.score
我称呼这个对象的方式:
def traceBack(self):
self.matrix = np.array(self.matrix)
maxIndex = self.matrix.argmax()
print(self.matrix.unravel_index(maxIndex))
MyObject
中,这样您将更容易获得帮助。self.score
是什么? - user2032433__int__()
,而你正在做的无论如何也不会调用它——我不相信Python会在比较对象时试图强制将它们转换为同一类型。 - millimoose