如何在Python中基于另一个列表删除列表中的项?

2024-09-30 12:26:37 发布

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

我有两张单子:

all_words_merged = ['ego', 'femina', 'incenderare', 'tuus', 'casa', 'et',
                    'cutullus', 'incipere', 'et', 'wingardium', 'leviosa']
class_words_merged = ['femina', 'incenderare', 'incipere', 'wingardium']

我想获取all_words_merged并删除发生在class_words_merged中的任何实例。结果列表应为:

result = ['ego', 'tuus', 'casa', 'et', 'cutullus', 'et', 'leviosa']

我尝试了下面的代码,但它返回了一个空列表:

result = [x for x in class_words_merged if x[0] in all_words_merged]

Tags: 列表mergedallclassetwordscasaego
3条回答

也可以使用filter内置方法来完成,如下所示:

>>> all_words_merged = ['ego', 'femina', 'incenderare', 'tuus', 'casa', 'et', 'cutullus', 'incipere', 'et', 'wingardium', 'leviosa']
>>> class_words_merged = ['femina', 'incenderare', 'incipere', 'wingardium']
>>>
>>> list(filter(lambda x: x not in class_words_merged, all_words_merged))
['ego', 'tuus', 'casa', 'et', 'cutullus', 'et', 'leviosa']

list对于Python3是必需的,因为filter生成一个过滤器对象,而在Python2中,这不是必需的,只是:

>>> filter(lambda x: x not in class_words_merged, all_words_merged)

编辑:

当然,这不是优化的方法,因为您必须将生成器转换为列表,您可以通过时序配置文件来猜测:

>>> timeit.timeit(stmt='list(filter(lambda x: x not in c, a))', globals={'a':all_words_merged, 'c':class_words_merged})
2.6026250364160717
>>> timeit.timeit(stmt='[x for x in a if x not in c]', globals={'a':all_words_merged, 'c':class_words_merged})
1.3826178676799827

您应该在all_words_merged上迭代,并且只包含不在class_words_merged中的单词

result = [x for x in all_words_merged if x not in class_words_merged]

输出:

['ego', 'tuus', 'casa', 'et', 'cutullus', 'et', 'leviosa']

编辑

如果class_words_merged可以包含重复项,那么首先使用set将提供更好的性能。你知道吗

cwm_set = set(class_words_merged)
result = [x for x in all_words_merged if x not in cwm_set]

如果class_words_merged很大,首先将其转换为一个集合会加快速度:

>>> to_remove = set(class_words_merged)
>>> [word for word in all_words_merged if word not in to_remove]
['ego', 'tuus', 'casa', 'et', 'cutullus', 'et', 'leviosa']

一些计时

100倍大:

large_class_words_merged = class_words_merged * 100

先创建为集合:

%%timeit
to_remove = set(large_class_words_merged)
[word for word in all_words_merged if word not in to_remove]
1000 loops, best of 3: 493 µs per loop

反复浏览清单:

%timeit [word for word in all_words_merged if word not in large_class_words_merged]
100 loops, best of 3: 3.18 ms per loop

提示:

%timeit%%imeit是我在Jupyter笔记本中使用的IPython魔术命令。你知道吗

相关问题 更多 >

    热门问题