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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。