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


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


设置 issuperset() 方法

issuperset() 方法用于检查这个集合(set1)是否是 set2 的超集,以 set1 和 set2 调用的方法作为参数提供,该方法返回 "True" 是 set1 中存在的 set2 的所有元素,否则该方法返回"False"。

用法:

    set1.issuperset(set2)

参数:

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

返回值:

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

范例1:

# Python Set issuperset() Method with Example

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

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

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

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

# checking using condition
if cars_1.issuperset(cars_2):
    print("cars_1 is superset of cars_2")
else:
    print("cars_1 is not superset of cars_2")

if cars_2.issuperset(cars_3):
    print("cars_2 is superset of cars_3")
else:
    print("cars_2 is not superset of cars_3")

if cars_1.issuperset(cars_3):
    print("cars_1 is superset of cars_3")
else:
    print("cars_1 is not superset of cars_3")

输出

cars_1.issuperset(cars_2): True
cars_2.issuperset(cars_3): False
cars_1.issuperset(cars_3): True
cars_1 is superset of cars_2
cars_2 is not superset of cars_3
cars_1 is superset of cars_3

范例2:

# Python Set issuperset() Method with Example

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

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

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

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

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

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

输出

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


相关用法


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