Python - 如果字符串包含列表或集合中的单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27287223/
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-19 01:37:03  来源:igfitidea点击:

Python - If string contains a word from a list or set

pythonstringset

提问by MoJo2015

I have searched quite thoroughly and have not found a suitable answer. I am new to Python/Programming, so I appreciate any advice I can get:

我已经搜索得很彻底,但没有找到合适的答案。我是 Python/编程的新手,所以我很感激我能得到的任何建议:

I am trying to search user input strings for certain key words. For example, we'll say filtering out profanity. From my research, I have been able to make the following dummy example:

我正在尝试搜索某些关键字的用户输入字符串。例如,我们会说过滤掉脏话。根据我的研究,我已经能够制作以下虚拟示例:

Swear = ("curse", "curse", "curse") #Obviously not typing actual swear words, created a set
Userinput = str.lower(input("Tell me about your day: "))

if Userinput in Swear:
     print("Quit Cursing!")
else:
     print("That sounds great!")

Using the above, if the user enters an exact word form the set as their entire string, it will print "quit cursing"; however, if the user enters "curses" or "I like to say curse" it will print "That sounds great!"

使用上面的方法,如果用户从集合中输入一个确切的单词作为他们的整个字符串,它将打印“quit cursing”;然而,如果用户输入“curses”或“我喜欢说curses”,它会打印“这听起来很棒!”

Ultimately what I need is to be able to search the entire string for a key word, not an exact match of the entire string. Ex: "I went to the park and felt like screaming curses" should return true for a match.

最终我需要的是能够在整个字符串中搜索关键字,而不是整个字符串的完全匹配。例如:“我去了公园,感觉像尖叫着诅咒”应该返回 true 以匹配比赛。

采纳答案by caleb.breckon

Swear = ["curse", "curse", "curse"]

for i in Swear:
    if i in Userinput:
        print 'Quit Cursing!'

You should read up on the differences between lists and tuples.

您应该仔细阅读列表和元组之间的差异。

回答by Tanveer Alam

Swear = ("curse", "curse", "curse") 
Userinput = str.lower(raw_input("Tell me about your day: "))

if any(Userinput.find(s)>=0 for s in Swear):
     print("Quit Cursing!")
else:
     print("That sounds great!")

Result:

结果

Tell me about your day: curse
Quit Cursing!

Tell me about your day: cursing
That sounds great!

Tell me about your day: curses
Quit Cursing!

Tell me about your day: I like curse
Quit Cursing!


Using Regular Expression:

使用正则表达式

Pattern used is r"\bcurse[\w]*".

使用的模式是r"\bcurse[\w]*"

Swear = ("curse", "curse", "curse") 
Userinput = str.lower(raw_input("Tell me about your day: "))

if any(match.group() for match in re.finditer(r"\bcurse[\w]*", Userinput)) :
     print("Quit Cursing!")
else:
     print("That sounds great!")


finditer(pattern, string, flags=0)
    Return an iterator over all non-overlapping matches in the
    string.  For each match, the iterator returns a match object.

    Empty matches are included in the result.

回答by Marcin

You can use sets, if only you want to check the existance of swear words,

你可以使用集合,如果你只想检查脏话的存在,

a_swear_set = set(Swear)

if a_swear_set & set(Userinput.split()):
     print("Quit Cursing!")
else:
     print("That sounds great!")