0
0

More than 1 year has passed since last update.

python3 和集合と積集合

Posted at

set型を使う

set型を使って和集合と積集合をもとめます。set型を作るときは{}で囲います。

s = {1,2,3}

set型の特徴は順番と重複が無視されます。

集合演算子

set型に集合演算子を使うと和集合や積集合が求められます。
| 和集合
& 積集合

コード

まずは和集合から

s = {1,2,3,4,5,5}#重複は無視される
t = {2,3}

union = s|t#集合sとtの和集合
print(union)

出力

{1, 2, 3, 4, 5}

積集合

s = {1,2,3,4,5,5}#重複は無視される
t = {2,3}

intarsection = s & t#sとtの積集合
print(intarsection)

出力

{2, 3}
0
0
1

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