Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
コンマで区切られた文字列があります。文字列内の重複するエントリをpythonicな方法で削除するにはどうすればよいですか。
たとえば、文字列"a,a,b"を に変更する必要があります"a,b"。
"a,a,b"
"a,b"
要素の順序は重要ですか? そうでない場合、最も簡単な方法は を作成することsetです:
set
result = ','.join(set(text.split(',')))
しかし、私が言ったように、それは元の文字列の順序を保持しません:
>>> text = 'b,a,b' >>> ','.join(set(text.split(','))) 'a,b'