Python setメソッドでリストからセットに変換するサンプル

環境
Python 3.9.13
Windows 10 Home 64bit
PyCharm 2022.2.1 (Community Edition)

構文
リスト名 =[要素1,要素2,…]
変数名 = set(リスト名)
setメソッドでリストをセットに変換しています。
重複している値は、削除されます。
値の順番はランダムです。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# coding: utf-8
s = ["to","os","11","to"]
res = set(s)
print (res)
# coding: utf-8 s = ["to","os","11","to"] res = set(s) print (res)
# coding: utf-8

s = ["to","os","11","to"]
res = set(s)
print (res)

実行結果
{’11’, 'to’, 'os’}

Python

Posted by arkgame