【问题标题】:Removing separate list of items from another list in Python 3.x从 Python 3.x 中的另一个列表中删除单独的项目列表
【发布时间】:2015-05-25 04:22:57
【问题描述】:

我有一个列表,其中包含许多标记的二元组。一些二元组未正确标记,因此我想将它们从主列表中删除。 bigrams 的一个词经常重复,所以如果它包含 xyz 词,我可以删除它。伪示例如下:

master_list = ['this is', 'is a', 'a sample', 'sample word', 'sample text', 'this book', 'a car', 'literary text', 'new book', 'them about', 'on the' , 'in that', 'tagged corpus', 'on top', 'a car', 'an orange', 'the book', 'them what', 'then how']

unwanted_words = ['this', 'is', 'a', 'on', 'in', 'an', 'the', 'them']

new_list = [item for item in master_list if not [x for x in unwanted_words] in item]

我可以单独删除项目,即每次我创建一个列表并删除包含单词的项目,比如,'on'。这很乏味,并且需要数小时的过滤和创建新列表来过滤每个不需要的单词。我认为循环会有所帮助。但是,我收到以下类型错误:

Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
new_list = [item for item in master_list if not [x for x in  unwanted_words] in item]
File "<pyshell#21>", line 1, in <listcomp>
new_list = [item for item in master_list if not [x for x in unwanted_words] in item]
TypeError: 'in <string>' requires string as left operand, not list

非常感谢您的帮助!

【问题讨论】:

    标签: loops python-3.x list-comprehension remove-if


    【解决方案1】:

    您的条件if not [x for x in unwanted_words] in itemif not unwanted_words in item 相同,即您正在检查 list 是否包含在 string 中。

    相反,您可以使用any 检查二元组的任何部分是否在unwanted_words 中。此外,您可以将 unwanted_words 设为 set 以加快查找速度。

    >>> master_list = ['this is', 'is a', 'a sample', 'sample word', 'sample text', 'this book', 'a car', 'literary text', 'new book', 'them about', 'on the' , 'in that', 'tagged corpus', 'on top', 'a car', 'an orange', 'the book', 'them what', 'then how']
    >>> unwanted_words = set(['this', 'is', 'a', 'on', 'in', 'an', 'the', 'them'])
    >>> [item for item in master_list if not any(x in unwanted_words for x in item.split())]
    ['sample word', 'sample text', 'literary text', 'new book', 'tagged corpus', 'then how']
    

    【讨论】:

    • 非常感谢您的帮助,tobias_k。它工作得很好。这就是我一直在寻找的。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-20
    相关资源
    最近更新 更多