gpt4 book ai didi

python - 在 Python 中使用列表方法与使用字符串方法

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:10 27 4
gpt4 key购买 nike

我有一个关于我发现的问题的快速问题。在 Python shell 中,我可以编写 test1 = "hello".upper(),当我键入 test1 时,我会得到 "HELLO",正如预期的那样.但是,如果我对列表执行类似的操作,例如 test2 = ["hello", "world"].reverse(),然后尝试返回 test2,我什么也得不到;它是一个没有赋值的“NoneType”。为什么会这样?为什么我可以用作用于字符串的方法进行赋值,但当有作用于列表的方法时我不能进行赋值?

最佳答案

是因为list.reverse()反转到位。这意味着它返回 None,因此当您执行 test2 = ["hello", "world"].reverse() 时,您分配的是 的结果.reverse() (None) 到 test2

关于python - 在 Python 中使用列表方法与使用字符串方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32654888/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com