找出段落中出现的单词
sentence = 'Alice was not a bit hurt, and she jumped up on to her feet in a moment.'
words = ['Alice','jumped','played']
我可以在Python中使用filter
函数来找到sentence
中出现的所有words
元素:
print filter(lambda x: x in words,sentence.split())
但是如果words
中的元素有空格,使用.split()
函数就会出错:
words = ['Alice','jumped up','played']
在这种情况下,'jumped up'
就无法在sentence
中找到,这样就不对了。
有没有简单的方法可以解决这个问题(也许re
包可以做到?)
1 个回答
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']