未分類

python3 でリストから重複を削除したリストを作成する方法

投稿日:

いったん dictionary に変換してから、新しいリストを作成する。
すると、重複を除いたリストが作れる。

list1 = ['1','2','3','1','2','5']
dict1 = dict.fromkeys(list1)
list2 = list(dict1)
print(list2)

結果

['1', '2', '3', '5']

-未分類

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

python3 で、指定した文字が最後に現れる場所を取得する

rfind を使うと、指定した文字が最後に現れる場所(インデックス)を取得することができる。 例 str1 = “12345abab” result = str1.rfind(“ab”) print( …

no image

tex で小文字のL(リットル)記号を入力する方法

tex で小文字のL(l)リットル記号を入力するには、\ell を使う。通常の小文字の l は、\\l であるのに注意。 例 5 \\ell 結果

no image

tex で和集合を入力する方法

\bigcup を使う。 例 $\displaystyle \bigcup _ {i \in \Lambda} A_i $ 結果

no image

python3 で、文字列を一文字ずつのリストに変換する方法

string を list() で変換すれば良い。 例 str1 = ‘Good morning.’ list1 = list(str1) print(list1) 結果 [‘G’, ‘o’, ‘o’ …

no image

tex で数式モード内に普通の文字列を入力する方法

\textrm を使う。 例 $$y = f (x) \hspace{1cm} \textrm{This is a function.}$$ 結果