python判断集合的方法有哪些?
1、isdisjoint方法用于判断两个集合是否存在相同元素,没有返回True,否则返回False。
my_set1={"apple","orange","pear","grape"}
my_set2={"banana","watermelon"}
#两个集合没有相同元素
ret_bool=my_set1.isdisjoint(my_set2)
print(ret_bool)#返回True
my_set1={"apple","orange","pear","grape"}
my_set2={"banana","watermelon","apple"}
#两个集合有相同元素
ret_bool=my_set1.isdisjoint(my_set2)
print(ret_bool)
2、issubset该方法用于判断一个集合是否是另一个集合的子集,确定是返回True,否则返回False。
my_set1={"apple","orange","pear","grape"}
my_set2={"banana","watermelon"}
#第二个集合不是第一个集合的子集
ret_bool=my_set2.issubset(my_set1)
print(ret_bool)#返回False
#第二个集合是第一个集合的子集
my_set1={"apple","orange","pear","grape"}
my_set2={"orange","apple"}
ret_bool=my_set2.issubset(my_set1)
print(ret_bool)#返回True
以上就是Python判断集合的方法,希望能对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。