找出段落中出现的单词

时间:2014-04-04 11:55:59

标签: python regex string python-2.7

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

我可以使用python中的filter函数查找words中显示的sentence中的所有元素:

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

但如果words中的元素中有空格,.split()函数会导致错误:

words = ['Alice','jumped up','played']

在这种情况下,'jumped up'中找不到sentence,这是不正确的。

是否有一个简单的方法可以解决问题(也许re包可以做到吗?)

1 个答案:

答案 0 :(得分:5)

您可以使用正则表达式:

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']