「Python」集合(set)で要素を追加、削除するサンプル

2020年12月10日

構文
集合(set)名.add(要素)
集合(set)名.remove(要素)

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
setA = {"A001","B002","C003"}
setA.add('DD04')
print("集合(set)Aに要素を追加: ",setA)
setB = {"TA01","TB02","TC03"}
setB.remove('TB02')
print("集合(set)Bに要素を削除: ",setB)
#!/usr/bin/ruby # -*- coding: UTF-8 -*- setA = {"A001","B002","C003"} setA.add('DD04') print("集合(set)Aに要素を追加: ",setA) setB = {"TA01","TB02","TC03"} setB.remove('TB02') print("集合(set)Bに要素を削除: ",setB)
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

setA = {"A001","B002","C003"}

setA.add('DD04')

print("集合(set)Aに要素を追加: ",setA) 

setB = {"TA01","TB02","TC03"}

setB.remove('TB02')

print("集合(set)Bに要素を削除: ",setB)

実行結果
>python test.py
>python test.py
集合(set)Aに要素を追加: {'B002’, 'DD04’, 'A001’, 'C003’}
集合(set)Bに要素を削除: {'TA01’, 'TC03’}

Python

Posted by arkgame