Python set isdisjoint() 函數檢查兩個集合是否不相交,如果不相交則返回 True,否則返回 False。當兩個集合的交集為空時,它們被稱為不相交。
Python設置isdisjoint()方法用法:
用法:set1.isdisjoint(set2)
參數:
- 另一組進行比較
或者 - 可迭代(列表、元組、字典和字符串)
返回:bool
Python 設置isdisjoint() 方法示例:
Python3
s1 = {1, 2, 3}
s2 = {4, 5, 6}
print(s1.isdisjoint(s2))
輸出:
True
示例 1:使用 Set isdisjoint() 方法
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
示例 2:使用其他可迭代對象作為參數的 Python Set isdisjoint()
Python3
# Set
A = {2, 4, 5, 6}
# List
lis = [1, 2, 3, 4, 5]
# Dictionary dict, Set is formed on Keys
dict = {1: 'Apple', 2: 'Orange'}
# Dictionary dict2
dict2 = {'Apple': 1, 'Orange': 2}
print("Set A and List lis disjoint?", A.isdisjoint(lis))
print("Set A and dict are disjoint?", A.isdisjoint(dict))
print("Set A and dict2 are disjoint?", A.isdisjoint(dict2))
輸出:
Set A and List lis disjoint? False Set A and dict are disjoint? False Set A and dict2 are disjoint? True
示例 3:兩個集合都是空的
在這裏,我們將看到如果我們使用 isdisjoint() 方法並且兩個集合均為空,將會輸出什麽。
Python3
# Python code to demonstrate
# isdisjoint method with blank
# sets
# defining empty set1
s1 = set()
# defining empty set2
s2 = set()
# using the isdisjoint method
# with empty sets
print(s1.isdisjoint(s2))
輸出
True
相關用法
- Python Set isdisjoint()用法及代碼示例
- Python Set issubset()用法及代碼示例
- Python Set issuperset()用法及代碼示例
- Python Set intersection()用法及代碼示例
- Python Set intersection_update()用法及代碼示例
- Python Set remove()用法及代碼示例
- Python Set add()用法及代碼示例
- Python Set copy()用法及代碼示例
- Python Set clear()用法及代碼示例
- Python Set difference()用法及代碼示例
- Python Set difference_update()用法及代碼示例
- Python Set discard()用法及代碼示例
- Python Set pop()用法及代碼示例
- Python Set symmetric_difference()用法及代碼示例
- Python Set symmetric_difference_update()用法及代碼示例
- Python Set union()用法及代碼示例
- Python Set update()用法及代碼示例
- Python Set轉String用法及代碼示例
- Python Seaborn.barplot()用法及代碼示例
- Python String format()用法及代碼示例
- Python String capitalize()用法及代碼示例
- Python String center()用法及代碼示例
- Python String casefold()用法及代碼示例
- Python String count()用法及代碼示例
- Python String endswith()用法及代碼示例
注:本文由純淨天空篩選整理自Striver大神的英文原創作品 Python Set isdisjoint() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。