python 示例
设置isdisjoint()方法 (Set isdisjoint() Method)
isdisjoint() method is used to check whether elements are common in two sets or not, method is called with one set (commonly known as this set) and another set is supplied as an argument, method returns "True" if none of the elements are common in both sets, else it returns "False".
isdisjoint()方法用于检查元素在两个集合中是否通用,用一个集合(通常称为此集合)调用方法,并提供另一个集合作为参数,如果没有元素,则方法返回“ True”在这两个集合中都是相同的,否则返回“ False”。
Syntax:
句法:
set1.isdisjoint(set2)
Parameter(s):
参数:
set1 – It represents the set1 (this set).
set1 –代表set1(此set)。
set2 – It represents the set2 (another set to be compared).
set2 –代表set2(另一个要比较的集合)。
Return value:
返回值:
The return type of this method is <class 'bool'>, it returns a Boolean value (True or False).
此方法的返回类型为<class'bool'> ,它返回一个布尔值(True或False)。
Example 1:
范例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)
Output
输出量
cars_1.isdisjoint(cars_2): False
cars_2.isdisjoint(cars_3): False
cars_1.isdisjoint(cars_3): True
Example 2:
范例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)
Output
输出量
x.isdisjoint(y): False
y.isdisjoint(z): True
z.isdisjoint(x): True
x.isdisjoint(z): True
y.isdisjoint(x): False
翻译自: https://www.includehelp.com/python/set-isdisjoint-method-with-example.aspx
python 示例