如何检查一个句子是否包含 Python 中的某个单词,然后执行一个动作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3897942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:15:14  来源:igfitidea点击:

How do I check if a sentence contains a certain word in Python and then perform an action?

pythoninput

提问by Noah R

Let's say that I ask a user for raw input and they said, "This is a message." If that raw input contained the word "message" it would perform an action after that. Could I see how this can be done?

假设我向用户询问原始输入,他们说:“这是一条消息。” 如果该原始输入包含“消息”一词,它将在此之后执行操作。我能看看这是怎么做到的吗?

采纳答案by Brent Writes Code

Going based on the comment by @knitti, the problem is that you need to split up the sentence into words first, then check:

根据@knitti 的评论,问题是你需要先把句子分成单词,然后检查:

term = "message" #term we want to search for
input = raw_input() #read input from user

words = input.split() #split the sentence into individual words

if term in words: #see if one of the words in the sentence is the word we want
    do_stuff()

Otherwise if you had the sentence "That one is a classic" and you tried to check if it contained the word "ass", it would return True incorrectly.

否则,如果您有句子“That one is a classic”并且您尝试检查它是否包含单词“ass”,它会错误地返回 True。

Of course, this still isn't perfect because then you might have to worry about things like stripping out punctuation and what not (like , . etc.) because otherwise the sentence "That one is a classic." would still return False for a search for "classic" (because of the period at the end). Rather than reinvent the wheel, here's a good post on stripping punctuation from a sentence in Python:

当然,这仍然不是完美的,因为那样你可能不得不担心诸如去掉标点符号之类的事情(比如,等等),否则这句话“那是经典之作”。仍然会返回 False 以搜索“经典”(因为末尾的句点)。这里有一篇关于从 Python 中的句子中去除标点符号的好文章,而不是重新发明轮子:

Best way to strip punctuation from a string in Python

在 Python 中从字符串中去除标点符号的最佳方法

There's case-sensitivity to consider too, so you might want to change the raw_inputresult and your search term to lowercase before doing a search. You could easily do that by just using the lower()function on the strclass.

还需要考虑区分大小写,因此您可能希望raw_input在进行搜索之前将结果和搜索词更改为小写。您只需使用类lower()上的函数即可轻松做到这一点str

These problems always seem to simple...

这些问题看似简单...

回答by Ned Batchelder

if "message" in sentence:
    do_action()

回答by cschol

This is of course a very simple example:

这当然是一个非常简单的例子:

if "message" in raw_input():
    action()

If you are required to map different words to different actions, then you could do something like this:

如果您需要将不同的词映射到不同的动作,那么您可以执行以下操作:

# actions
def action():
    print "action"

def other_action():
    print "other action"

def default_action():
    print "default action"

# word to action translation function
def word_to_action(word):
    return {
        "message":  action,
        "sentence": other_action
    }.get(word, default_action)()

# get input, split into single words
w = raw_input("Input: ").split()

# apply the word to action translation to every word and act accordingly
map(word_to_action, w)

Note, that this also defines a default action for the case when the input does not contain any of the trigger words.

请注意,当输入不包含任何触发词时,这也定义了默认操作。

See herefor more details on the above mapping idiom, which is actually Python's way of achieving 'switch statement'.

有关上述映射习惯用法的更多详细信息,请参见此处,这实际上是 Python 实现“switch 语句”的方式。