「Python」set()メソッドでリストの重複要素を取り除くサンプル

2020年12月10日

構文
set(リスト名)
リストから重複した要素を取り除くことができますが、元のリストの順序は保持されない。
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
cft= [11, 22, 33, 22, 33, 55]
result = list(set(cft))
print("結果:",result)
#!/usr/bin/ruby # -*- coding: UTF-8 -*- cft= [11, 22, 33, 22, 33, 55] result = list(set(cft)) print("結果:",result)
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

cft= [11, 22, 33, 22, 33, 55]

result = list(set(cft))

print("結果:",result)

実行結果
>python test.py
結果: [33, 11, 22, 55]

Python

Posted by arkgame