我試圖編寫一個讀取文件的代碼,然後返回文件中所有迴文列表。所以我創建了一個函數來檢查一個單詞是否是一個迴文,並且我試圖編寫另一個函數來讀取文件,擺脫空白區域,分割成單詞,然後測試每個單詞以查看它是否是一個迴文。如果它是一個迴文,那麼我將它添加到我將打印的列表中。但是,我收到一個錯誤「AttributeError:'元組'對象沒有屬性'append'」我怎樣才能得到添加到列表中的迴文?在文件中搜索palindromes並使用python打印它們列表
def findPalindrome(filename):
#an empty list to put palindromes into
list3 =()
#open the file
for line in open(filename):
#strip the lines of blank space
list1 = line.strip()
#Split the lines into words
list2 = line.split()
#call one of the words
for x in list2:
#test if it is a palindrome
if isPalindrome(x):
#add to list3
list3.append(x)
#return the list of palindromes
return list3