如何在Python中检查一个列表是否包含另一个列表的所有元素?它不起作用

Sha*_*cob 2 python python-3.x

list1 = ['Gryffindor', 'Ravenclaw', 'Hufflepuff', 'Slytherin']
list2 = ['Gryffindor', 'Ravenclaw']

checkif = item in List2 for item in List1

if check is True:
    print("The list {} contains all elements of the list {}".format(List1, List2))
Run Code Online (Sandbox Code Playgroud)

为什么这个该死的东西不起作用?也是checkif = item in list2 for item in list1列表理解还是什么?

有人请纠正我的代码,谢谢。

Z4-*_*ier 7

我想你想要all

list1 = ['Gryffindor', 'Ravenclaw', 'Hufflepuff', 'Slytherin']
list2 = ['Gryffindor', 'Ravenclaw']

checkif = all(item in list1 for item in list2)
Run Code Online (Sandbox Code Playgroud)

此外,您需要交换list1list1以获得您在该行中描述的结果print