python is用法_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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值