2 回答
TA贡献1846条经验 获得超7个赞
你已经完成了:
from collections import Counter
def isPalindrome(s):
if (s == s[::-1] ): return True
else: return False
def findPalindrome():
list3=list()
for line in open("English.txt"):
list1 = line.strip()
list2 = list1.split()
for x in list2:
if isPalindrome(x):
list3.append(x)
return list3
palim_list = findPalindrome()
palim_counts = Counter(palim_list)
print palim_list
print palim_counts
TA贡献1851条经验 获得超5个赞
看起来你让事情复杂化了,要从列表中的文件中获取所有回文,你可以迭代它的空行剥离内容并检查字符串是否等于它的反转表示:
一些文件:
kaak
okay
tired
alot
acca
因此:
res = []
with open('someFile.txt', 'r') as fObj:
content = [l.strip() for l in fObj.readlines() if l.strip()]
for line in content:
if line == line[::-1]:
res.append(line)
print(res)
输出:
['kaak', 'acca']
添加回答
举报