找出段落中出现的单词

2 投票
1 回答
1861 浏览
提问于 2025-04-18 01:20
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']

撰写回答