检查列表列表的所有元素是否在另一个列表列表Python中 [英] Checking if all elements of a List of Lists are in another List of Lists Python

查看:775
本文介绍了检查列表列表的所有元素是否在另一个列表列表Python中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,如何检查列表列表的所有元素是否都在另一个列表列表中?或许用更好的措辞来说,您如何检查列表的一个列表是否是列表的另一个的子集?以下发布的答案仅在您具有字符串列表的情况下才有效,但该答案不适用于我的情况.

My question is, how do you check if all of the elements of a list of lists are in another list of lists? Or maybe better phrased, how do you check if one list of lists is a subset of another list of lists? The answer posted below only works if you have a list of, say, strings but the answer does not work for my situation.

如何检查列表中的所有项目是否在另一个列表中?

我尝试过的事情是这样的:

Something I have tried is something like this:

if all(item in list1 for item in list2): 

不起作用.

推荐答案

将子列表转换为元组,例如:

Convert your sublists to tuples, for example:

In [2]: a = [[2,3],[5,6],[8,9]]

In [3]: b = [[2,3],[5,6],[8,9], [10,11]]

In [4]: set(tuple(x) for x in a).issubset(tuple(x) for x in b)
Out[4]: True

这篇关于检查列表列表的所有元素是否在另一个列表列表Python中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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