查找两个嵌套列表之间的差异 [英] Finding the differences between two nested lists
本文介绍了查找两个嵌套列表之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个嵌套列表和另一个嵌套列表,它是第一个列表的子集:
I have a nested list and another nested list which is a subset of the first list:
lst = [[1, 2], [3, 4], [1, 2], [5, 6], [8, 3], [2, 7]]
sublst = [[1, 2], [8, 3]]
如何找到不在子列表中的内部列表.使用以上示例的期望输出是:
How can I find the inner lists which are not in the sublist. The desired output using the above example is:
diff = [[3, 4], [5, 6], [2, 7]]
推荐答案
使用列表理解:
In [42]: lst = [[1, 2], [3, 4], [1, 2], [5, 6], [8, 3], [2, 7]]
In [43]: sublst = [[1, 2], [8, 3]]
In [44]: [x for x in lst if x not in sublst]
Out[44]: [[3, 4], [5, 6], [2, 7]]
或filter()
:
In [45]: filter(lambda x:x not in sublst,lst)
Out[45]: [[3, 4], [5, 6], [2, 7]]
这篇关于查找两个嵌套列表之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文