当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Set isdisjoint()用法及代码示例


设置 isdisjoint() 方法

isdisjoint() 方法用于检查两个集合中的元素是否相同,以一个集合(通常称为这个集合)调用方法,并提供另一个集合作为参数,如果两个集合中没有一个元素是公共的,则方法返回 "True"设置,否则返回 "False"。

用法:

    set1.isdisjoint(set2)

参数:

  • set1– 它代表了 set1(这个 set)。
  • set2– 它代表 set2(另一个要比较的集合)。

返回值:

这个方法的返回类型是<class 'bool'>,它返回一个布尔值(真或假)。

范例1:

# Python Set isdisjoint() Method with Example

# declaring the sets
cars_1 = {"Porsche", "Audi", "Lexus"}
cars_2 = {"Porsche", "Mazda", "Lincoln"}
cars_3 = {"BMW", "Mazda", "Lincoln"}

# isdisjoint() method call
result = cars_1.isdisjoint(cars_2)
print("cars_1.isdisjoint(cars_2):", result)

result = cars_2.isdisjoint(cars_3)
print("cars_2.isdisjoint(cars_3):", result)

result = cars_1.isdisjoint(cars_3)
print("cars_1.isdisjoint(cars_3):", result)

输出

cars_1.isdisjoint(cars_2): False
cars_2.isdisjoint(cars_3): False
cars_1.isdisjoint(cars_3): True

范例2:

# Python Set isdisjoint() Method with Example

# declaring the sets
x = {"ABC", "PQR", "XYZ"}
y = {"ABC", "PQR", "XYZ"}
z = {"DEF", "MNO", "UVW"}

# isdisjoint() method calls
result = x.isdisjoint(y)
print("x.isdisjoint(y):", result)

result = y.isdisjoint(z)
print("y.isdisjoint(z):", result)

result = z.isdisjoint(x)
print("z.isdisjoint(x):", result)

result = x.isdisjoint(z)
print("x.isdisjoint(z):", result)

result = y.isdisjoint(x)
print("y.isdisjoint(x):", result)

输出

x.isdisjoint(y): False
y.isdisjoint(z): True
z.isdisjoint(x): True
x.isdisjoint(z): True
y.isdisjoint(x): False


相关用法


注:本文由纯净天空筛选整理自 Python Set isdisjoint() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。