为什么我的回文检查器找不到任何结果?

发布于 2025-02-12 02:19:00 字数 324 浏览 2 评论 0 原文

words = ['Anna', 'Alexey', 'Alla', 'Kazak', 'Dom']

def palindrome():
    filtered_2 = list(filter(lambda x: x == x[::-1], words))
    print(filtered_2)

palindrome()

该代码应返回palindrome单词,但它为我提供了这样的输出:

[]

它应该给出:

['Anna', 'Alla', 'Kazak']
words = ['Anna', 'Alexey', 'Alla', 'Kazak', 'Dom']

def palindrome():
    filtered_2 = list(filter(lambda x: x == x[::-1], words))
    print(filtered_2)

palindrome()

The code should return palindrome words, but it gives me an output like this:

[]

It should give:

['Anna', 'Alla', 'Kazak']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尴尬癌患者 2025-02-19 02:19:01

如果使用python> = 3.8,则可以使用:= )将原始单词保留在结果中,并且只有一个 .lower()每个单词呼叫:

    filtered_2 = [x for x in words if (y := x.lower()) == y[::-1]]

If using Python >= 3.8, it is possible to use the walrus operator (:=) to keep the original words in the result and have only one .lower() call per word:

    filtered_2 = [x for x in words if (y := x.lower()) == y[::-1]]
眉目亦如画i 2025-02-19 02:19:01

让我还强调一个事实,即函数的定义 palindrome ,就像您的代码中毫无意义,并且脚本在不定义函数的情况下完全相同:

words = ['Anna', 'alexey', 'Alla', 'kazak', 'dom']
_words=map(str.lower,words)
filtered_2 = list(filter(lambda x: x == x[::-1], _words))
print(filtered_2)

但是,如果您想用函数来制作函数是简单的方法:

def palindrome(words_list):
   _words=map(str.lower,words)
   filtered = list(filter(lambda x: x.lower() == x.lower()[::-1], words))
   return filtered

这样,此代码

words = ['Anna', 'Alexey', 'Alla', 'Kazak', 'Dom']
palindrome(words)

将返回您想要的结果。

Let me also highlight the fact that the definition of the function palindrome as in your code is pointless and the script would work exactly the same without defining the function:

words = ['Anna', 'alexey', 'Alla', 'kazak', 'dom']
_words=map(str.lower,words)
filtered_2 = list(filter(lambda x: x == x[::-1], _words))
print(filtered_2)

But if you want to make it with a function this is the simples way:

def palindrome(words_list):
   _words=map(str.lower,words)
   filtered = list(filter(lambda x: x.lower() == x.lower()[::-1], words))
   return filtered

In this way this code

words = ['Anna', 'Alexey', 'Alla', 'Kazak', 'Dom']
palindrome(words)

would return the results you want.

微暖i 2025-02-19 02:19:00

在比较之前,您需要将输入标准化。基本上,您正在检查'a'=='a'是否会失败。

尝试使用 str.lower()

words = ['Anna', 'alexey', 'Alla', 'kazak', 'dom']

def palindrome():
    filtered_2 = list(filter(lambda x: x.lower() == x.lower()[::-1], words))
    print(filtered_2)

palindrome()

You need to normalize the input before comparisons. Basically you are checking if 'A' == 'a' which will fail.

Try this example where I normalize to lowercase using str.lower():

words = ['Anna', 'alexey', 'Alla', 'kazak', 'dom']

def palindrome():
    filtered_2 = list(filter(lambda x: x.lower() == x.lower()[::-1], words))
    print(filtered_2)

palindrome()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文