回文功能 [英] palindrome function
问题描述
大家好,
有人可以向我解释为什么以下评估为假?
>>> list = ['''',''n'',''n'',''a'']
list == list.reverse()
False
我很难过:s
2008年7月12日星期六上午12:22,kdt< do ******** @ googlemail.comwrote:
< blockquote class =post_quotes>
大家好,
有人可以向我解释为什么以下评估为假吗?
>>>> list = ['''',''n'',''n'',''a'']
list == list.reverse()
False
我很难过:s
阅读文档list.reverse()。
基本上,它会反转列表,因此它会修改名为
的列表。它没有返回一个/ new / list,它是原版的反转版本,如你预期的那样.b $ b。由于它没有明确地返回任何内容,因此Python使其返回None。因此,您所做的比较是在原始列表与无之间进行比较,当然这是假的。
试试这个:
spam = ['''','n'','n'',''a'']
eggs = spam [:]
如果spam.reverse()== eggs:
print" Palindrome"
另外,''list''是一个非常糟糕的名字列表,因为这是列表类型的内置类型对象
的名称。
-
Denis Kasak
7月11日,11:34 * pm,Denis Kasak< denis.kasak2718281 ... @ gmail.com>
写道:
2008年7月12日星期六上午12:22,kdt< dorjeta ... @ googlemail.comwrote:
*大家好,
*>
*有人可以向我解释为什么以下评估为假吗?
*> ;
*>>>> list = ['''',''n'',''n'',''a ']
*>>>> list == list.reverse()
*>>>> False
*>
*我很难过:s
阅读list.reverse()上的文档。
基本上,它会反转列表,因此它会修改名为
的列表。它没有返回一个/ new / list,它是原版的反转版本,如你预期的那样.b $ b。由于它没有明确地返回任何内容,因此Python使其返回None。因此,您所做的比较是在原始列表与无之间进行比较,当然这是假的。
试试这个:
spam = ['''','n'','n'',''a'']
eggs = spam [:]
如果spam.reverse()== egg:
* * print" Palindrome"
另外,''list''真是太糟糕了列表的名称,因为这是列表类型的内置类型对象的名称
。
-
Denis Kasak
感谢您的解释:D
Denis Kasak:
spam = ['''',''n'','n'',''a'']
eggs = spam [:]
如果spam.reverse()== egg:
print" Palindrome"
替代版本:
>> txt =" anna"
txt == txt [:: - 1]
真
>> txt =" annabella"
txt == txt [:: - 1]
False
再见,
bearophile
Hi all,
Can someone please explain to me why the following evaluates as false?
>>>list=[''a'',''n'',''n'',''a'']
list==list.reverse()
False
I''m stumped :s
On Sat, Jul 12, 2008 at 12:22 AM, kdt <do********@googlemail.comwrote:Hi all,
Can someone please explain to me why the following evaluates as false?
>>>>list=[''a'',''n'',''n'',''a'']
list==list.reverse()
False
I''m stumped :sRead the documentation on list.reverse().
Basically, it reverses the list in place, so it modifies the list which
called it. It does not return a /new/ list which is a reversed version
of the original, as you expected it to. Since it doesn''t return anything
explicitly, Python makes it return None. Hence, the comparison you are
doing is between the original list and a None, which is False, naturally.
Try this:
spam = [''a'', ''n'', ''n'', ''a'']
eggs = spam[:]
if spam.reverse() == eggs:
print "Palindrome"
Also, ''list'' is a really bad name for a list, since this is the name of
the builtin type object for the list type.
--
Denis Kasak
On Jul 11, 11:34*pm, Denis Kasak <denis.kasak2718281...@gmail.com>
wrote:On Sat, Jul 12, 2008 at 12:22 AM, kdt <dorjeta...@googlemail.comwrote:
*Hi all,
*>
*Can someone please explain to me why the following evaluates as false?
*>
*>>>>list=[''a'',''n'',''n'',''a'']
*>>>>list==list.reverse()
*>>>>False
*>
*I''m stumped :s
Read the documentation on list.reverse().
Basically, it reverses the list in place, so it modifies the list which
called it. It does not return a /new/ list which is a reversed version
of the original, as you expected it to. Since it doesn''t return anything
explicitly, Python makes it return None. Hence, the comparison you are
doing is between the original list and a None, which is False, naturally.
Try this:
spam = [''a'', ''n'', ''n'', ''a'']
eggs = spam[:]
if spam.reverse() == eggs:
* * print "Palindrome"
Also, ''list'' is a really bad name for a list, since this is the name of
the builtin type object for the list type.
--
Denis Kasakthanks for the explanation :D
Denis Kasak:spam = [''a'', ''n'', ''n'', ''a'']
eggs = spam[:]
if spam.reverse() == eggs:
print "Palindrome"An alternative version:
>>txt = "anna"
txt == txt[::-1]
True
>>txt = "annabella"
txt == txt[::-1]
False
Bye,
bearophile
这篇关于回文功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!