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
包可以做到吗?)
答案 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']