【发布时间】:2020-05-30 15:42:07
【问题描述】:
我需要在遍历三个 LIST 后创建一个字典。 用于匹配句子(list_sent as KEYs)和单词列表(list_wordset as VALUEs)进行匹配 关键字(list_keywords)。请参阅下面的列表和预期的输出字典以及解释。请建议。
list_sent = ['one more shock like Covid-19',
'The number of people suffering acute',
'people must collectively act now',
'handling the novel coronavirus outbreak',
'After a three-week nationwide',
'strengthening medical quarantine']
list_wordset = [['people','suffering','acute'],
['Covid-19','Corona','like'],
['people','jersy','country'],
['novel', 'coronavirus', 'outbreak']]
list_keywords = ['people', 'Covid-19', 'nationwide','quarantine','handling']
“Covid-19”关键字出现在 list_sent 和 list_wordset 中,因此它们被收录在 Dictionary 中。 'people' 关键字出现在 list_sent 中的 2 个不同项目和 list_wordset 中的 2 个不同列表中,因此需要捕获它们。即使 list_wordset 中的单个单词与关键字匹配,那么它也是匹配的。
预期输出为:
out_dict =
{'one more shock like Covid-19': ['Covid-19','Corona','like'],
'The number of people suffering acute': [['people','suffering','acute'],['people','jersy','country']],
'people must collectively act now' : [['people','suffering','acute'],['people','jersy','country']]}
【问题讨论】:
-
list_keywords 中的所有关键字都不存在于 list_wordset 中。只有“人”和“Covid-19”在场。所以输出字典应该只有来自 list_sent(作为 KEY)和 list_wordset(作为 VALUE)的这些匹配项。请提出建议。
标签: python list dictionary list-comprehension python-re