Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图实现这个回文输出(python语言)。我试过这个_Python_Jupyter Notebook_Stack - Fatal编程技术网

试图实现这个回文输出(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)

这回答了你的问题吗?你试过调试这个吗?我建议阅读以下文章:。