2013-02-01 53 views
-2

我需要解析一个文件,它具有以下内容: result(x1 y1 x2 y2... xn yn) 这是我写的python中的一段代码。我是python的新手。for循环与Python中的多个变量

xs =[] ys=[] 

def __init__(self,xs,ys): 
     self.xs = xs 
     self.ys = ys 


def toString(self): 
    return "result " + " (" + " ".join([x.toString()+ " " + y.toString() for x,y in zip(self.xs, self.ys)]) + ") " 

我在运行时出现AttributeError: 'str' object has no attribute 'toString'错误。请建议如何解决这个问题。

+0

只要确保:所有这一切都是在课堂上正确的?因为否则'自我'没有意义 – alonisser

+0

是的。第一行写在调用之前。解析结果(x1 x2 ... xn)我有下面的代码,它工作正常。 def toString(self): return“result”+“”.join([x.toString()for x in self.xs])+“)”我需要用这种形式写。 – user1727270

回答

2

Python中的字符串没有toString()方法。您可以改用str(your_variable)。如果变量已经是字符串,则不需要做任何特殊的事情。