2013-02-01 54 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)。如果變量已經是字符串,則不需要做任何特殊的事情。