比较两个列表以删除不需要的单词

2024-05-14 18:04:55 发布

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

我需要将一个字符串或列表与另一个不需要的单词列表进行比较,以便只留下关键字,例如:

>filter_words(['how', 'about', 'i', 'go', 'through', 'that', 'little', 'passage', 'to', 'the', 'south'], skip_words)
>> Expected outcome ['go', 'passage', 'south']

这需要与跳过单词进行比较,即:

>skip_words = ['a', 'about', 'all', 'an', 'another', 'any', 'around', 'at',
              'bad', 'beautiful', 'been', 'better', 'big', 'can', 'every', 'for',
              'from', 'good', 'have', 'her', 'here', 'hers', 'his', 'how',
              'i', 'if', 'in', 'into', 'is', 'it', 'its', 'large', 'later',
              'like', 'little', 'main', 'me', 'mine', 'more', 'my', 'now',
              'of', 'off', 'oh', 'on', 'please', 'small', 'some', 'soon',
              'that', 'the', 'then', 'this', 'those', 'through', 'till', 'to',
              'towards', 'until', 'us', 'want', 'we', 'what', 'when', 'why',
              'wish', 'with', 'would']

这个例子使用一个单词列表而不是一个基本的字符串,所以这是一个初步的函数,而不是最终的结果,但我还没有找到一个解决方案。你知道吗


Tags: theto字符串go列表that单词how
1条回答
网友
1楼 · 发布于 2024-05-14 18:04:55

您应该阅读python文档以获得列表理解,以下代码段应该会有所帮助:

newlist = []

for i in filter_words:
    if i not in skip_words:
        newlist.append(i)

相关问题 更多 >

    热门问题