【发布时间】:2014-05-16 17:29:38
【问题描述】:
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包可以搞定?)
【问题讨论】:
标签: python regex string python-2.7