Python 字符串方法难题
以下代码应该在删除 SpamWords[0] 后打印 MyWords。然而;它不返回“是”,而是返回“无”。为什么它返回“无”?
MyWords = "Spam yes"
SpamWords = ["SPAM"]
SpamCheckRange = 0
print ((MyWords.upper()).split()).remove(SpamWords[SpamCheckRange])
The following code is supposed to print MyWords after removing SpamWords[0]. However; instead of returning "yes" it instead returns "None". Why is it returning "None"?
MyWords = "Spam yes"
SpamWords = ["SPAM"]
SpamCheckRange = 0
print ((MyWords.upper()).split()).remove(SpamWords[SpamCheckRange])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为
remove
是一种更改其调用的可变列表对象的方法,并返回None
。也许你想要:
Because
remove
is a method that changes the mutable list object it's called on, and returnsNone
.Perhaps you want:
remove
是list
的方法(str.split
返回一个列表),而不是str
。它会改变原始列表(删除您传递的内容)并返回None
,不是修改后的列表。remove
is a method oflist
(str.split
returns a list), notstr
. It mutates the original list (removing what you pass) and returnsNone
, not a modified list.