我需要将一个字符串或列表与另一个不需要的单词列表进行比较,以便只留下关键字,例如:
>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']
这个例子使用一个单词列表而不是一个基本的字符串,所以这是一个初步的函数,而不是最终的结果,但我还没有找到一个解决方案。你知道吗
您应该阅读python文档以获得列表理解,以下代码段应该会有所帮助:
相关问题 更多 >
编程相关推荐