【问题标题】:How to Create a Python Dictionary after iterating over three Lists for matching words如何在迭代三个列表以匹配单词后创建 Python 字典
【发布时间】: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


【解决方案1】:
>>> {sent: [
    wordset for wordset in list_wordset if any(word in sent for word in wordset)
] for sent in list_sent}
{'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']], 
 'handling the novel coronavirus outbreak': [['novel', 'coronavirus', 'outbreak']], 
 'After a three-week nationwide': [], 
 'strengthening medical quarantine': []}

【讨论】:

  • 我们可以使用 list_keywords 中的关键字进行匹配吗?幸运的是,上面的解决方案适用于这个例子。但我想依赖 list_keywords 中的关键字。请提出建议。
  • 目前还不清楚那会是什么样子,因为您希望输出以“wordsets”的形式出现。据我所知,“关键字”是完全多余的,因为您关心的所有关键字也将在词集中。
  • list_keywords 中的所有关键字都不存在于 list_wordset 中。只有“人”和“Covid-19”在场。所以输出字典应该有这些来自 list_sent 和 list_wordset 的匹配项。预期的输出匹配解决方案中的前 3 个键/值对,但不匹配最后 3 个键/值对。请建议在此过程中也考虑 list_keywords。
【解决方案2】:

我能够使用所有 3 个列表,以字典格式创建所需的输出。要删除空值,请使用附加步骤。

out_dict = {sent: [wordset for wordset in list_wordset if any(key in sent and key in wordset for key in list_keywords)]
 for sent in list_sent}

结果:

{'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']],
 'handling the novel coronavirus outbreak': [],
 'After a three-week nationwide': [],
 'strengthening medical quarantine': []}

删除空列表值:

out_dict = dict( [(k,v) for k,v in out_dict.items() if len(v)>0])

最终结果:

{'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']]}

【讨论】:

    猜你喜欢
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多