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培訓機構:千鋒教育。