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


Python isdisjoint()用法及代码示例


当两个集合的交集为空时,它们是不相交的。简而言之,它们之间没有任何共同的元素。

例子:

Let set A = {2, 4, 5, 6}
and set B = {7, 8, 9, 10} 

set A and set B are said to be be disjoint sets as their
intersection is null. They do-not have any common elements in between them. 

用法:


set1.isdisjoint(set2)

参数:

isdisjoint()方法仅接受一个参数。它还可以对disjoint()进行迭代(列表,元组,字典和字符串)。 isdisjoint()方法将自动将可迭代对象转换为集合,并检查集合是否不相交。

返回值:

returns Trueif the two sets are disjoint.
returns Falseif the twos sets are not disjoint.

以下是上述方法的Python3实现:

# Python3 program for isdisjoint() function 
  
set1 = {2, 4, 5, 6}  
set2 = {7, 8, 9, 10} 
set3 = {1, 2} 
  
  
#checking of disjoint of two sets 
print("set1 and set2 are disjoint?", set1.isdisjoint(set2)) 
  
print("set1 and set3 are disjoint?", set1.isdisjoint(set3))

输出:

set1 and set2 are disjoint? True
set1 and set3 are disjoint? False


相关用法


注:本文由纯净天空筛选整理自Striver大神的英文原创作品 isdisjoint() function in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。