问题描述
我的问题与上面的差不多:
my question is pretty much as above:
如果您有以下两个列表,如何检查第一个列表中的所有项目是否都在第二个列表中.例如
if you have 2 lists as below, how can you check if all items in the first list are in the second list.eg
list_one=[1,2,3]
list_two=[1,2,3,4]
我当前的尝试只是如果list_two中的所有list_one:"
my current attempt is just "if all list_one in list_two:"
但是这种情况似乎从未得到满足,因此没有任何进一步发生.任何帮助将不胜感激,谢谢:)
but this condition never seems to be filled and so nothing further takes place. Any help would be appreciated thanks :)
推荐答案
all()
函数用于检查是否满足所有条件.我们从list_1获取元素,并检查是否满足条件如果全部可用,则在list_2中可用,然后我们打印是"
The all()
function is used to check if all the condition are satisfied .We are getting elements from list_1 and checking if that is available in list_2 if all are available then we print "yes"
list_one=[1,2,3]
list_two=[1,2,3,4]
if all(a in list_two for a in list_one):
print "yes"
这篇关于如何检查子列表中的所有项目是否都在列表中?Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!