从句子列表和单词列表中,仅当所有三个单词都与单词列表(三叉图)匹配时,如何返回句子列表
请建议。下面是示例列表
listwords = [['people','suffering','acute'], ['Covid-19','Corona','like'], ['people','must','collectively']]
listsent = ['The number of people suffering acute hunger could almost double.',
'Lockdowns and global economic recession have',
'one more shock – like Covid-19 – to push them over the edge',
'people must collectively act now to mitigate the impact']
输出列表应位于第一位&;最后的句子,因为它们在listwords中有三个匹配的单词
预期产出为:
['The number of people suffering acute hunger could almost double.',
'people must collectively act now to mitigate the impact']
欢迎来到堆栈溢出
请尝试以下解决方案:
我对这些行进行了评论,让你了解发生了什么
代码的第一部分遍历列表中的每个句子
然后我们需要遍历单词列表中的单词组
这就是事情变得有趣的地方。因为你有嵌套列表,我们需要检查以确保在句子中找到所有三个单词
最后,您可以打印出包含所有单词的每个句子
您还可以将其放入函数中,并将找到的句子作为新列表返回
相关问题 更多 >
编程相关推荐