如何创建一个关键字是词干,值是带有该词干的单词列表的字典?

2024-09-25 18:25:28 发布

您现在位置:Python中文网/ 问答频道 /正文

在python中,我已经有了单词列表和词干列表。如何创建一个字典,其中键是词干,值是带有该词干的单词列表,如下所示:

{‘achiev’: [‘achieved’, ‘achieve’] ‘accident’: [‘accidentally’, ‘accidental’] … }


Tags: 列表字典单词词干accidentachieveachievedaccidental
2条回答

您可以使用dict comprehnsionlist comprehnsion使用一行代码来实现这一点:

{stem: [w for w in words if w.startswith(stem)] for stem in stems}
stems = ['accident', 'achiev']
words = ['achieved', 'accidentally', 'achieve', 'accidental']

results = {}
for stem in stems:
    stem_words = [w for w in words if w.startswith(stem)]
    results[stem] = stem_words

print(results)

印刷品:

{'accident': ['accidentally', 'accidental'], 'achiev': ['achieved', 'achieve']}

相关问题 更多 >