我希望能够检查一个列表是否包含另一个列表的2个元素(总共包含3个元素)
例如:
list1 = ["a", "b", "c"]
list2 = ["a", "f", "g", "b"]
if #list2 contains any 2 elements of list1:
print("yes, list 2 contains 2 elements of list 1")
else:
print("no, list 2 does not contain 2 elements of list 1")
最佳答案
我将使用以下问题中描述的类似描述,仅这次检查集合交集的长度:
How to find list intersection?
list1 = ["a", "b", "c"]
list2 = ["a", "f", "g", "b"]
if len(list(set(a) & set(b))) == 2:
print("yes, list 2 contains 2 elements of list 1")
else:
print("no, list 2 does not contain 2 elements of list 1")
关于python - 如何检查一个列表是否包含另一个列表的2个元素?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58593768/