跳到主要内容

Python3 集合 isdisjoint() 方法

描述

isdisjoint() 方法用于判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False。。

语法

isdisjoint() 方法语法:

set.isdisjoint(set)

参数

  • set -- 必需,要比较的集合

返回值

返回布尔值,如果不包含返回 True,否则返回 False。

实例

判断集合 y 中是否有包含 集合 x 的元素:

实例 1

x = {"apple", "banana", "cherry"}
y = {"google", "W3Cschool", "facebook"}

z = x.isdisjoint(y)

print(z)#不包含返回true

输出结果为:

True

如果包含返回 False:

实例 2

x = {"apple", "banana", "cherry"}
y = {"google", "W3Cschool", "apple"}

z = x.isdisjoint(y)

print(z)

输出结果为:

False