2015-05-04 89 views
0

Python中有一种方法可以搜索输入字符串中的短语,然后返回如果有,则为1;否则为0?在Python中搜索短语的字符串输入

我希望它是这样工作的:

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.") 
+0

完全可能的。看看're'模块文档。 – cge

+0

还是'in'运营商,它告诉你一个字符串是否包含在另一个。 –

+0

@ Rozza_15如果您的查询已解决,请不要忘记[接受下面的答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work):) –

回答

2

您当前的实现是越野车,将无法正常工作。

  1. .search是一个函数,它是一个对象。由于它是一个对象,它永远不会等于True。因此,你总是会返回0
  2. findphrase('well')(howis)中的代码是无效的语法,因为你不从Python2 findphrase
  3. input将评估的声明,以及,这将引发NameError S代表字符串输入返回的功能。因此改用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() 

and call i t等:

>>> 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 
+0

只是好奇为什么在这里选择拉姆达?我知道这只是个人喜好,但我很少使用它们,除非我有一个特定的理由来**使用它们。 –

+0

@DougR。 http://stackoverflow.com/q/890128/1860929 –

+0

感谢您的参考。 –

1

一个正则表达式可能是这个矫枉过正。我会用in

例1:

我实现findphrase()的方式,给你返回的需求的10是:

>>> 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() FUNC重刑,你有一些错误。你打电话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()给变量var,你要findphrase()提供的变量phrase,并存储为not_well()命名空间的一部分。

但是在这种情况下,您可能真正想要做的是使用两个参数来定义您的findphrase()函数,如前两个示例之一。

您还使用findphrase(...) is 1。你可能想有什么findphrase(...) == 1,或者甚至更Python,if findphrase(...):