比较 2 个项目列表,如果不在则引发错误

Comparing 2 lists of items and raising error if not in

我有 2 个字符串列表需要比较。

list1["a", "b", "c", "d"]list2["a", "b", "c", "d", "e", "f"] 我成功地将它们与

进行了比较
check = any(i in ccourt for i in champs)
print(check) 

编辑:好的,所以我问得不好并且误解了 any() 方法,抱歉,我坚持的是比较部分。 是的,any() 部分有效,但它并没有说 list2"e""f" 不在 list1 中,这就是我的意思希望它做到。

提前致谢

lst1 = ["a", "b", "c", "d"]

list2 = ["a", "b", "c", "d", "e", "f"]

for i in list2:
    if i not in lst1:
        print(i, 'is not in lst1')

>>> e is not in lst1
>>> f is not in lst1

map:

list(map(lambda i: print(i, 'is not in lst1') if i not in lst1 else None, list2))

>>> e is not in lst1
>>> f is not in lst1

您可以使用“设置”:

set(list2) - set(list1)

结果:

set(['e', 'f']) # printed output

set.difference也可以。

set(list1).difference(list2)