0
1

More than 3 years have passed since last update.

python set型

Posted at

python set型

pythonのset型とは、集合を扱うための型
リスト型のように、set型も同じく複数の値を格納できる型

リスト型との違い
・重複した要素を持たない
・要素に順番がない

set型の基本的な使い方
set_1 = {1, 2, 3, 4}
print(set_1)
list_a = [1, 2, 4, 6, 8]
set_2 = set(list_a)
print(set_2)

メソッド
add : setに要素を追加する。
remove : setから要素を取り除く。指定した要素が無いときはエラーが出る。
discard : 指定した要素がsetに含まれている場合、それを取り除く。要素が含まれていなかったときはスルーする。

set.add
set.remove
set.discard

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