试图实现这个回文输出(python语言)。我试过这个
我正在尝试让我的代码打印此输出 这是我的密码试图实现这个回文输出(python语言)。我试过这个,python,jupyter-notebook,stack,Python,Jupyter Notebook,Stack,我正在尝试让我的代码打印此输出 这是我的密码 outputlist = [] bad_character=['! ', ','] def is_palindrome(alist): for element in alist: for s in word: if s is bad_character: continue my_stack.append(s.lower)) my_stack_reverse=my_stack[::-1]
outputlist = []
bad_character=['! ', ',']
def is_palindrome(alist):
for element in alist:
for s in word:
if s is bad_character:
continue
my_stack.append(s.lower))
my_stack_reverse=my_stack[::-1]
if my_stack_reverse==my_stack
outputlist.append(True)
else
append(False)
if my_stack_reverse=my_stack
test_list=['Madam', 'A nut for a jar of Tuna', 'I love DSAG']
print(is_palindrome(test_list))
print(test_list)
我试图将此作为输出,但输出时出错:
[对,对,错]
[‘夫人’、‘一罐金枪鱼的坚果’、‘我爱DSAG’]下面的代码满足了您的要求。确保您的坏字符列表中包含空格(“”)。我使用replace()方法使坏字符的删除变得更干净,您可以一次对整个字符串使用lower()方法,而不是逐个字符进行删除。此外,您还可以通过附加比较
element==element[::-1]
跳过函数末尾的if/else,因为这将生成所需的布尔值
def ispalindrome(alist):
outputlist = []
bad_character = [' ']
for element in alist:
for character in bad_character:
element = element.replace(character, '')
element = element.lower()
outputlist.append(element == element[::-1])
return outputlist
test_list = ['Madam', 'A nut for a jar of Tuna', 'I love DSAG']
print(ispalindrome(test_list))
print(test_list)
这回答了你的问题吗?你试过调试这个吗?我建议阅读以下文章:。