2015-05-04 87 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(...):