检查列表是否包含另一个列表中的所有项目 [英] Checking if List contains all items from another list
本文介绍了检查列表是否包含另一个列表中的所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
检查元组列表是否包含另一个列表执行的所有元组的最简单/最快的方法是什么?例如:
What is the easiest/fastest way to check if a list of tuples contains all the the tuples that another list does. For example:
t1 = [ (1,2), (3,4), (5,6), (7,8), (9,10), (11,12) ]
t2 = [ (3,4), (11,12) ]
将是一个真实的情况,因为t1包含t2中的所有元组.我尝试过类似的事情:
would be a true case since t1 contains all the tuples in t2. I tried something like:
[i for e in t2 for i in t1 if e in i]
sorted(t1) == sorted(t2)
,但这似乎总是返回true.有更好的方法吗?
but this seemed to always return true. Is there a better way?
推荐答案
您可以使用 set
s
t1 = [ (1,2), (3,4), (5,6), (7,8), (9,10), (11,12) ]
t2 = [ (3,4), (11,12) ]
set(t2).issubset(t1)
# returns true
# or equivalent use '<=' so
set(t2) <= set(t1)
# returns true
这篇关于检查列表是否包含另一个列表中的所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文