程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python tutorial] part 50 superset of set

編輯:Python

In this article, we introduce how to use issuperset() Method to determine whether a set is a superset of another set .

issuperset() Method introduction

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.

issuperset() Method example

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

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.

summary

  • If the collection B All elements of belong to the collection A, aggregate A Namely B Superset .
  • A collection of issuperset() Method is used to determine whether a set is a superset of another set .
  • Superset operator (>=) And the true superset operator (>) You can also judge whether a set is a superset or a true superset of another set .

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved