在Python中搜索字符串输入中的短语

2024-06-30 08:31:45 发布

您现在位置:Python中文网/ 问答频道 /正文

Python中有没有一种方法可以在输入字符串中搜索一个短语,然后返回一个1(如果有),或者0(如果没有)?你知道吗

我希望它像这样工作:

def findphrase(var):
if re.compile(r'\b({0})\b'.format(var), flags=re.IGNORECASE).search is True:
    return 1
else:
    return 0

def howareyou():
    print("So",name, "how are you today?")
    howis = input("")
    if findphrase('not well')(howis) is 1:
        print("Oh, that's not good. I hope you feel better soon")
    elif findphrase('well')(howis) is 1:
        print("That's good.")
    elif findphrase('not bad')(howis) is 1:
        print("Better than bad, I suppose.")
    elif findphrase('bad')(howis) is 1:
        print("Oh, that's not good. I hope you feel better soon")
    elif findphrase('not good')(howis) is 1:
        print("That's a shame. I hope you feel better soon.")
    elif findphrase('good')(howis) is 1:
        print("That's good.")
    else:
        print("I dont know how to respond to that. Keep in mind I am a work in progress. At some point I may know how to respond.")

Tags: youthatisnothowfeelgoodprint
2条回答

您当前的实现有bug,无法工作。你知道吗

  1. .search是一个函数,它是一个对象。既然它是一个对象,它就永远不等于真。因此,您将始终返回0。你知道吗
  2. ^代码中的{}是无效语法,因为您没有从findphrase返回函数
  3. ^Python2中的{}也将对语句求值,这将为字符串输入抛出NameError。所以用raw_input代替。你知道吗
  4. 您可以很容易地使用in操作符来反对在这里使用正则表达式
  5. if findphrase('good')(howis) is 1:是一个身份测试,因为您只返回0/1,所以可以直接使用if findphrase('good')(howis):检查值

您可以在这里使用一个简单的lambda函数:

findphrase = lambda s, var: var.lower() in s.lower()

把它叫做:

>>> findphrase("I'm not well", "Not Well")
True
>>> findphrase("I'm not well", "Good")
False

如果你想返回一个函数, 那你可以用

findphrase = lambda var: lambda original_string: var.lower() in original_string.lower()

>>> howis = raw_input()
I'm doing GooD
>>> findphrase("Good")(howis)
True

正则表达式在这方面可能有点过头了。我会用in。你知道吗

例1:

考虑到返回10的要求,我实现findphrase()的方法是:

>>> def findphrase(phrase, to_find):
...   if to_find.lower() in phrase.lower():
...     return 1
...   else:
...     return 0
... 
>>> phrase = "I'm not well today."
>>> to_find = 'not well'
>>> in_phrase = findphrase(phrase, to_find)
>>> assert in_phrase == 1
>>> 

注意使用to_find.lower()phrase.lower()来确保资本化并不重要。你知道吗

例2:

但坦白说,我不知道你为什么要返回1或0。我只需要返回一个布尔值,这将使:

>>> def findphrase(phrase, to_find):
...   return to_find.lower() in phrase.lower()
... 
>>> phrase = "I'm not well today."
>>> to_find = "not well"
>>> in_phrase = findphrase(phrase, to_find)
>>> assert in_phrase == True
>>> 

如果您确实需要将结果用作10(如果您重写howareyou()函数,则不会这样做),则TrueFalse分别转换为10

>>> assert int(True) == 1
>>> assert int(False) == 0
>>>

附加说明

howareyou()函数中,有许多错误。您将findphrase()称为findphrase('not well')(howis)。只有从findphrase()(闭包)返回函数时,这才有效,如下所示:

>>> def findphrase(var):
...     def func(howis):
...         return var.lower() in howis.lower()
...     return func
... 
>>> phrase = "I'm not well today."
>>> to_find = "not well"
>>> in_phrase = findphrase(to_find)(phrase)
>>> assert in_phrase == True
>>> 

这是因为函数只是Python中的另一种类型的对象。它可以像任何其他对象一样返回。如果您按照以下思路进行操作,则可能需要使用这样的构造:

>>> def findphrase(var):
...     def func(howis):
...         return var.lower() in howis.lower()
...     return func
...
>>> phrases = ["I'm not well today.",
...            "Not well at all.",
...            "Not well, and you?",]
>>>
>>> not_well = findphrase('not well')
>>>
>>> for phrase in phrases:
...     in_phrase = not_well(phrase)
...     assert in_phrase == True
...
>>>

这是因为将findphrase('not well')的结果赋给变量not_well。这将返回一个函数,然后可以将其作为not_well(phrase)调用。执行此操作时,它会将提供给not_well()的变量phrase与提供给findphrase()的变量var进行比较,该变量作为not_well()命名空间的一部分存储。你知道吗

但在本例中,您可能真正想做的是用两个参数定义findphrase()函数,就像前两个示例中的一个。你知道吗

你也在使用findphrase(...) is 1。你可能想要的是findphrase(...) == 1或者,更像Python的,if findphrase(...):。你知道吗

相关问题 更多 >