In this article, we introduce how to use issuperset() Method to determine whether a set is a superset of another set .
For collection A and B, If B All elements in belong to A, that A Namely B Superset or superset of (superset). here , aggregate B Namely A Subset (subset). If the collection A It's not equal to the set B,A Namely B It's a super collection .
Logically speaking , Any set is its own superset . The set in the following figure A yes B Superset , Because of the collection B The elements in 1、2、3 All belong to the collection A.
stay Python Can use set issuperset() Method to determine whether a set is a superset of another set :
set_a.issuperset(set_b)
If set_a yes set_b Superset ,issuperset() Method returns True; otherwise , return False.
The following example uses issuperset() Method to determine the set numbers Is it a collection scores Superset :
numbers = {
1, 2, 3, 4, 5}
scores = {
1, 2, 3}
result = numbers.issuperset(scores)
print(result)
The output is as follows :
True
Because of the collection scores All elements in belong to the set numbers, numbers yes scores Superset .
Any set is its own superset , for example :
numbers = {
1, 2, 3, 4, 5}
result = numbers.issuperset(numbers)
print(result)
The output is as follows :
True
aggregate scores No numbers Subset , So the following example returns False:
numbers = {
1, 2, 3, 4, 5}
scores = {
1, 2, 3}
result = scores.issuperset(numbers)
print(result)
False
Superset operator (>=) Used to determine whether a set is a superset of another set :
set_a >= set_b
If the collection set_a Is a collection set_b Superset , The superset operator returns True; otherwise , return False. for example :
numbers = {
1, 2, 3, 4, 5}
scores = {
1, 2, 3}
result = numbers >= scores
print(result) # True
result = numbers >= numbers
print(result) # True
True superset operator (>) It is used to judge whether a set is a true superset of another set :
set_a > set_b
for example :
numbers = {
1, 2, 3, 4, 5}
scores = {
1, 2, 3}
result = numbers > scores
print(result) # True
result = numbers > numbers
print(result) # True
In the example above , aggregate numbers Not its own true superset , therefore > The operator returns False.