【发布时间】:2014-12-28 00:00:34
【问题描述】:
我正在尝试编写一个代码来读取文件,然后返回文件中所有回文的列表。所以我创建了一个函数来检查一个单词是否是回文,并且我尝试编写另一个函数来读取文件,摆脱空格,拆分成单词,然后测试每个单词以查看它是否是回文。如果它是回文,那么我将它添加到我将在最后打印的列表中。但是,我收到一个错误“AttributeError: 'tuple' object has no attribute 'append'” 我怎样才能将回文添加到这个列表中?
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
【问题讨论】:
标签: python list append text-files