我找到了以下代码,但是当我尝试运行代码时,它显示了SyntaxError,我不知道为什么

时间:2020-04-06 12:35:15

标签: python-3.x

我想逐步看一下代码,看看它是如何工作的,如果有人可以帮助我运行代码,我将不胜感激。

def isPalindrome(word:str)-> bool: if(word == word [::-1]): 返回True 返回Falsedef getPalindromesFromStr(inputStr:str)->列表: cleanStr = inputStr.replace(“,”,“”)。lower() 单词= set(cleanStr.split(“”)) wPalindromes = [ 逐字逐句 如果isPalindrome(word)和单词!=“” ] return wPalindromesgetPalindromesFromStr(“大声笑,这是一个插科打,,我很久没笑了很多”)

1 个答案:

答案 0 :(得分:0)

在这里我要实话实说:这段代码很糟糕。都是一行,缺少空格,列表继续。

但是,如果格式正确,则下面是代码的外观。这应该发挥作用。

def isPalindrome(word):
    return word == word[::-1]

def getPalindromesFromStr(inputStr):
    cleanStr = inputStr.replace(",","").lower()
    words = set(cleanStr.split(" ")) 
    wPalindromes = [word for word in words if isPalindrome(word) and word != "" ]
    return wPalindromes

print(getPalindromesFromStr("Lol, this is a gag, I didn’t laugh so much in a long time"))