0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで「集合と他の集合との関係を調べる(等しいかどうか、部分集合かどうかなど)」の動作を確認してみた

Posted at

概要

Pythonで「集合と他の集合との関係を調べる(等しいかどうか、部分集合かどうかなど)」の動作を確認してみました。
以下のページを参考にしました。

実装

以下のファイルを作成しました。

set1 = {"A", "B", "C"}
set2 = {"B", "C", "A"}
print(set1 == set2)

set1 = {"A", "B", "C"}
set2 = frozenset(["B", "A", "C"])
print(set1 == set2)

set1 = {"A", "B"}
set2 = {"B", "D", "C", "A"}
set3 = {"B", "A"}
print(set1 <= set2)
print(set2 <= set1)
print(set1 <= set3)

set1 = {"A", "B"}
set2 = {"B", "D", "C", "A"}
set3 = {"A", "B"}
print(set1.issubset(set2))
print(set2.issubset(set1))
print(set1.issubset(set3))

set1 = {"A", "B"}
set2 = {"B", "D", "C", "A"}
set3 = {"A", "B"}
print(set1 < set2)
print(set2 < set1)
print(set1 < set3)

set1 = {"A", "B"}
set2 = {"B", "D", "C", "A"}
set3 = {"B", "A"}
print(set1 >= set2)
print(set2 >= set1)
print(set1 >= set3)
print(set3 >= set1)

set1 = {"A", "B"}
set2 = {"B", "D", "C", "A"}
set3 = {"A", "B"}
print(set1.issuperset(set2))
print(set2.issuperset(set1))
print(set1.issuperset(set3))
print(set3.issuperset(set1))

set1 = {"A", "B"}
set2 = {"B", "D", "C", "A"}
set3 = {"A", "B"}
print(set1 > set2)
print(set2 > set1)
print(set1 > set3)

set1 = {"A", "B"}
set2 = {"C", "D", "E"}
set3 = {"A", "C"}
print(set1.isdisjoint(set2))
print(set1.isdisjoint(set3))
print(set2.isdisjoint(set3))

以下のコマンドを実行しました。

$ python3 sample.py 
True
True
True
False
True
True
False
True
True
False
False
False
True
True
True
False
True
True
True
False
True
False
True
False
False

まとめ

何かの役に立てばと。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?