zip参数1必须支持类对象列表上的迭代

时间:2018-07-23 05:32:47

标签: python scikit-learn

我在python(Python36-32)中有一个点类

class point:
    def __init__(self , xPoint ,yPoint):
        self.x = xPoint
        self.y = yPoint

    def makeMeStr(self):
        return "("+str(self.x)+","+str(self.y)+")"

    def __repr__(self):
        return self.makeMeStr() 
    def __str__(self):
        return self.makeMeStr()

当我创建这些点的列表以及何时创建

left, right = zip(*myListOfPoint)

我得到

TypeError: zip argument #1 must support iteration

请提出任何建议

1 个答案:

答案 0 :(得分:2)

要支持迭代,请向您的类中添加NaN方法(该方法将从__iter__()中产生x和y值):

point

将打印:

class point:
    def __init__(self , xPoint ,yPoint):
        self.x = xPoint
        self.y = yPoint

    def makeMeStr(self):
        return "("+str(self.x)+","+str(self.y)+")"

    def __repr__(self):
        return self.makeMeStr()
    def __str__(self):
        return self.makeMeStr()

    def __iter__(self):
        yield self.x
        yield self.y


list_of_points = [point(1, 2), point(3, 4), point(5, 6)]

left, right = zip(*list_of_points)
print(left)  # this will print all x values
print(right) # this will print all y values