python - 如何检查一个列表是否包含另一个列表的 2 个元素?

标签 python python-3.x list function

我希望能够检查一个列表是否包含另一个列表的 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/

相关文章:

python - 我想找到一天中办公室最高峰的时间

python - Pandas lambda 多个参数返回

python - Anaconda 提示快速关闭

java - 如何为每个打印的 "line"创建列表或数组列表

python csv 在文件中查找文本,然后查找特定列

python - 用python删除sql代码并从JOIN中选择

.net - 数组与列表的性能

Python从字典列表中获取多个特定的键和值

python从正则表达式中获取子字符串

python - 如何使用 OpenCV 将 mp4 文件 FPS 值 30 更改为 5