Python字符串方法难题

以下代码应该在删除SpamWords [0]后打印MyWords.然而;而不是返回“是”而是返回“无”.为什么它会返回“无”?

MyWords = "Spam yes"
SpamWords = ["SPAM"]
SpamCheckRange = 0
print ((MyWords.upper()).split()).remove(SpamWords[SpamCheckRange])

最佳答案 因为remove是一个更改调用它的可变列表对象的方法,并返回None.

l= MyWords.upper().split()
l.remove(SpamWords[SpamCheckRange])
# l is ['YES']

也许你想要:

>>> [word for word in MyWords.split() if word.upper() not in SpamWords]
['yes']
点赞