Summary of Methods for Judging Set Range in python

  • 2021-09-16 07:26:19
  • OfStack

When we compare the values, we will use 1 comparison symbol to judge. There are also such comparisons in python collections, but one thing to note is that we compare the inclusiveness between collections, not the size comparison between simple values, which is made clear at the beginning of the article, and it is also a reminder for us beginners of python.

Collection can be used with a value greater than ( > ), less than ( < ), greater than or equal to ( > =), less than or equal to ( < =), equal to (= =), not equal to (! =) to determine whether a set is completely contained in another set, or you can use the child parent set judgment function.

Define three collections s1, s2, s3:


>>> s1 = set([1, 2, 3, 4, 5])
>>> s2 = set([1, 2, 3, 4])
>>> s3 = set(['1', '8', '9', '5'])

1. Greater than ( > ) or greater than or equal to ( > =)


>>> s1 > s2
True
>>> s1 > s3
False
>>> s1 >= s2
True

Indicates whether the left collection completely contains the right collection, such as whether the collection s1 completely contains the collection s2.

2. Less than ( < ) or less than or equal to ( < =)


>>> s2 < s1
True
>>> s1 < s3
False‍
>>> s3 < s1
False

Indicates whether the collection on the left is fully contained in the collection on the right, such as whether the collection s1 is fully contained in the collection s2.

3. Equal to (= =), not equal to (! =)


>>> s1 == s2
False
>>> s2 == s3
False
>>> s1 != s2
True

Determines whether two sets are identical.

Note: The size operator of the collection will only judge the inclusiveness of the collection! ! !

For example:


s1 = {1, 2, 3}
s2 = set(range(10))
print(s1 < s2) # True
print(s1 <= s2) # True
s3 = {4, 5, 6}
print(s3 > s1) # False
print(s2 >= s3) # True

Let's look at the method of python judging collection through sample code, as follows:

1. The isdisjoint method is used to determine whether there are identical elements in two sets, and True is not returned, otherwise False is returned.


my_set1 = {"apple", "orange", "pear", "grape"}
my_set2 = {"banana", "watermelon"} 
#  Two collections have no identical elements 
ret_bool = my_set1.isdisjoint(my_set2)
print(ret_bool) #  Return  True
my_set1 = {"apple", "orange", "pear", "grape"}
my_set2 = {"banana", "watermelon","apple"}
#  Two collections have the same element 
ret_bool = my_set1.isdisjoint(my_set2)
print(ret_bool)

2. issubset This method is used to judge whether one set is a subset of another set, and return True if it is determined, otherwise return False.


my_set1 = {"apple", "orange", "pear", "grape"}
my_set2 = {"banana", "watermelon"}
#  No. 1 2 A collection is not the first 1 A subset of a set 
ret_bool = my_set2.issubset(my_set1)
print(ret_bool) #  Return  False

#  No. 1 2 A set is the first 1 A subset of a set 
my_set1 = {"apple", "orange", "pear", "grape"}
my_set2 = {"orange","apple"}

ret_bool = my_set2.issubset(my_set1)
print(ret_bool) #  Return  True

Related articles: