1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python Set型について

1
Posted at

set型とは?

複数の値を格納できる型のこと。

リスト型との違いは
・重複した要素がない
・明確な順番がない

使い方

"""
myset1 = set([1,2,3])

print(myset1)

出力
{1, 2, 3}

"""

要素を追加するにはaddを使用します。

myset.add(4)

出力
{1, 2, 3, 4}

削除はremoveを使用します

myset.remove(3)

出力
{1, 2, 4}

すべてを削除するにはclear()を使います

myset.clear()

setが使えると、集合計算が簡単にできます。

・和集合(intersection)
2つのset型オブジェクトの要素を全て持った新しいset型オブジェクトのことを、和集合といいます。

・積集合(difference)
2つのset型オブジェクトの要素で、どちらの集合にも存在している要素のみを持った新しいset型オブジェクトのことを、積集合といいます。

・差集合
2つのset型オブジェクトA,Bの要素で、AからAとBの積集合を削除した要素のみを持った新しいset型オブジェクトを、差集合と呼びます。

・排他的論理和集合
2つのset型オブジェクトA,Bの要素で、AとBで共通した要素以外を足し合わせた新しいset型オブジェクトを、排他的論理和集合と呼びます。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?