比较字符串,找到每个字符串中存在的部分
如何比较几行,并找到每行中存在的单词的单词/组合?使用纯Python,NLTK或其他任何东西。
few_strings = ('this is foo bar', 'this is not a foo bar', 'some other foo bar here')
# some magic
result = 'foo bar'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将每个字符串分开在白空间,然后将结果单词保存到集合中。然后,计算三组的交点:
输出:
Split each string at whitespaces and save the resulting words into sets. Then, compute the intersection of the three sets:
Output:
您可能需要使用标准库
fifflib
进行序列比较,包括查找常见子字符串:You might want to use the standard library
difflib
for sequence comparisons including finding common substrings:“”
)创建一组现在,您有一个python
set
单词的单词>,这在所有句子中发生。您可以将集合转换为列表:
或与
" "
)result
variable with the interesction of the current result and one sentenceNow you have a Python
Set
of words which occured in all sentences.You can convert the set to list with:
or to string with
您也可以在不使用库的情况下做到这一点
You can do it without using libraries too