如何删除列表中位于python第二个列表中的项目? [英] How to remove the items in a list that are in a second list in python?

查看:100
本文介绍了如何删除列表中位于python第二个列表中的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表列表和另一个列表,我想从第二个列表中的列表列表中删除所有项目.

I have a list of lists, and another list, and I want to remove all of the items from the list of lists that are in the second list.

first = [[1,3,2,4],[1,2],[3,4,2,5,1]]
to_remove = [1,2]

一般来说,我将如何解决这个问题?

How would I go about doing this problem in general?

这里也有类似的问题,但没有任何帮助.

There are also similar questions on here but nothing has helped.

推荐答案

使用sets的优雅解决方案:

first = [[1,3,2,4],[1,2],[3,4,2,5,1]]
to_remove = [1,2]
result = [list(set(x).difference(to_remove)) for x in first]
result
[[3, 4], [], [3, 4, 5]]

这篇关于如何删除列表中位于python第二个列表中的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆