我的代码如下。我是没有经验的编码员,我认为问题出在.join(reversed())
零件上。但是,我不知道为什么。我需要其他人再检查一次,也许我错过了一些东西。
def is_palindrome(alist):
truth = []
for i in alist:
i.lower()
i.replace(" ","")
x = "".join(reversed(i))
if i == x:
truth.append(True)
else:
truth.append(False)
return truth
test_list = ['Stats', 'A nut for a jar of Tuna', 'I eat apples']
print(is_palindrome(test_list))
print(test_list)
给出的结果是:
[False, False, False]
应该是:
[True,True,False]
有人对我错了吗?
在lower()
和replace()
就地功能都没有。
您必须将其返回值分配给i
。
i=i.lower()
i=i.replace(" ","")
输出:
[True, True, False]
['Stats', 'A nut for a jar of Tuna', 'I eat apples']
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句