找出段落中出现的词 [英] find out the words appeared in a paragraph
本文介绍了找出段落中出现的词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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
中查找所有元素:
I can use the filter
function in python to find all the elements from words
shown in sentence
:
print filter(lambda x: x in words,sentence.split())
但是如果words
中的元素有空格,.split()
函数就会报错:
But if there is a space in the elements in words
, .split()
function causes mistake:
words = ['Alice','jumped up','played']
在这种情况下,sentence
中找不到'jumped up'
,这是错误的.
in this case, 'jumped up'
cannot be found in sentence
, which is incorrect.
有没有简单的方法可以解决这个问题(也许re
包可以做到?)
Is there a simple method that can deal with the problem (perhaps re
package can make it?)
推荐答案
您可以为此使用正则表达式:
You can use regex for this:
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']
这篇关于找出段落中出现的词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文