當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。