首页 > 解决方案 > 为什么在以下代码中使用 set 命令有好处?

问题描述

在下面的代码中,没有重复,这意味着我看不出为什么要使用 set 命令。

代码:

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"))

标签: python-3.x

解决方案


推荐阅读