2014-04-04 98 views
1
sentence = 'Alice was not a bit hurt, and she jumped up on to her feet in a moment.' 
words = ['Alice','jumped','played'] 

我可以使用filter功能在Python中找到wordssentence所示的所有元素:找出詞出現在一個段落

print filter(lambda x: x in words,sentence.split()) 

但如果在零件的空間words.split()功能導致的錯誤:

words = ['Alice','jumped up','played'] 
在這種情況下

'jumped up'不能在012找到,這是不正確的。

是否有可以處理該問題的簡單方法(可能是re包能做到嗎?)

回答

4

您可以使用正則表達式是:

In [71]: import re 

In [72]: words = ['Alice','jumped','played'] 

In [73]: [w for w in words if re.search(r'\b{}\b'.format(re.escape(w)), sentence)] 
Out[73]: ['Alice', 'jumped'] 

In [74]: words = ['Alice','jumped up','played'] 

In [75]: [w for w in words if re.search(r'\b{}\b'.format(re.escape(w)), sentence)] 
Out[75]: ['Alice', 'jumped up'] 
+0

+1使用're.escape( )'否則會是一團糟。 –

+0

感謝您的意見。它完美的作品。但是你能解釋一下re.escape()嗎?我想它的目的是處理字符串中的空間?但是\ b也包含空間考慮因素。 – ChuNan

+1

@ChuNan're.escape'會逃避任何被正則表達式認爲是特殊的字符,例如'.','*'等等。 –