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中找到words
在sentence
所示的所有元素:找出詞出現在一個段落
print filter(lambda x: x in words,sentence.split())
但如果在零件的空間words
,.split()
功能導致的錯誤:
words = ['Alice','jumped up','played']
在這種情況下
,'jumped up'
不能在012找到,這是不正確的。
是否有可以處理該問題的簡單方法(可能是re
包能做到嗎?)
+1使用're.escape( )'否則會是一團糟。 –
感謝您的意見。它完美的作品。但是你能解釋一下re.escape()嗎?我想它的目的是處理字符串中的空間?但是\ b也包含空間考慮因素。 – ChuNan
@ChuNan're.escape'會逃避任何被正則表達式認爲是特殊的字符,例如'.','*'等等。 –