简体   繁体   English

比较句子列表和单词列表,如果单词存在,则返回完整的句子

[英]Compare List of Sentences and List of words and return complete Sentences , if word is present

From the List of Sentences and List of words, how to return the list of complete Sentences, if a word is present.从句子列表和单词列表中,如果存在单词,如何返回完整句子的列表。 Please suggest.请建议。 Below are example lists.以下是示例列表。

listwords = ['people',  'Covid-19',  'Lockdowns', 'Maximum' ]

listsent = ['The number of people suffering acute hunger could almost double.',
        'It is potentially catastrophic for millions',
        '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']

'people' is present in two sentences, 'Covid-19' present in one sentences and 'Lockdown' in one sentence. “人”出现在两个句子中,“Covid-19”出现在一个句子中,“锁定”出现在一个句子中。 output list should have these four matching complete sentences from listsent. output 列表应该有这四个匹配的完整句子。

List comprehension is a quick way to do this:列表理解是一种快速的方法:

[sentence for sentence in listsent if any(word in sentence for word in listwords)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM