Pythonチュートリアル

Pythonホーム Pythonイントロ Pythonはじめに Python構文 Pythonコメント Python変数 Pythonデータ型 Python番号 Pythonキャスティング Python文字列 Pythonブール値 Python演算子 Pythonリスト Pythonタプル Pythonセット Python辞書 Python If ... Else PythonのWhileループ PythonForループ Python関数 Python Lambda Python配列 Pythonクラス/オブジェクト Pythonの継承 Pythonイテレータ Pythonスコープ Pythonモジュール Pythonの日付 Python数学 Python JSON Python RegEx Python PIP Python試してみてください... Pythonユーザー入力 Python文字列フォーマット

ファイル処理

Pythonファイル処理 Python読み取りファイル Python書き込み/ファイルの作成 Pythonファイルの削除

Pythonモジュール

NumPyチュートリアル パンダ攻略 Scipyチュートリアル

Python Matplotlib

Matplotlibイントロ Matplotlibはじめに Matplotlib Pyplot Matplotlibプロット Matplotlibマーカー Matplotlibライン Matplotlibラベル Matplotlibグリッド Matplotlibサブプロット Matplotlib散布図 Matplotlibバー Matplotlibヒストグラム Matplotlib円グラフ

機械学習

入門 平均中央値モード 標準偏差 パーセンタイル データ配信 正規データ分布 散布図 線形回帰 多項式回帰 重回帰 規模 トレーニング/テスト デシジョンツリー

Python MySQL

MySQLはじめに MySQLデータベースの作成 MySQLテーブルの作成 MySQL挿入 MySQL Select MySQL Where MySQL Order By MySQL削除 MySQLドロップテーブル MySQLアップデート MySQLの制限 MySQL参加

Python MongoDB

MongoDBはじめに MongoDBデータベースの作成 MongoDBCreateコレクション MongoDBインサート MongoDB検索 MongoDBクエリ MongoDBソート MongoDB削除 MongoDBドロップコレクション MongoDBアップデート MongoDBの制限

Pythonリファレンス

Pythonの概要 Python組み込み関数 Python文字列メソッド Pythonリストメソッド Python辞書メソッド Pythonタプルメソッド Pythonセットメソッド Pythonファイルメソッド Pythonキーワード Pythonの例外 Python用語集

モジュールリファレンス

ランダムモジュール リクエストモジュール 統計モジュール 数学モジュール cMathモジュール

Pythonハウツー

リストの重複を削除する 文字列を逆にする 2つの数字を追加する

Pythonの例

Pythonの例 Pythonコンパイラ Python演習 Pythonクイズ Python証明書

Pythonリストから重複を削除する方法


Pythonでリストから重複を削除する方法を学びます。


リストから重複を削除します。

mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)

例の説明

まず、重複を含むリストがあります。

重複のあるリスト

mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)

リストアイテムをキーとして使用して、辞書を作成します。辞書は重複キーを持つことができないため、これにより重複が自動的に削除されます。

辞書を作成する

mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)

次に、辞書をリストに戻します。

リストに変換する

mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)

これで、重複のないリストが作成され、元のリストと同じ順序になりました。

リストを印刷して結果を示します

リストを印刷する

mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
print(mylist)


関数を作成する

リストを送信して重複せずに戻すことができる関数が必要な場合は、関数を作成して上記の例のコードを挿入できます。

def my_function(x):
  return list(dict.fromkeys(x))

mylist = my_function(["a", "b", "a", "c", "c"])

print(mylist)

例の説明

リストを引数として取る関数を作成します。

関数を作成する

def my_function(x):
  return list(dict.fromkeys(x))

mylist = my_function(["a", "b", "a", "c", "c"])

print(mylist)

このリスト項目をキーとして使用して、辞書を作成します。

辞書を作成する

def my_function(x):
  return list(
dict.fromkeys(x))

mylist = my_function(["a", "b", "a", "c", "c"])

print(mylist)

辞書をリストに変換します。

リストに変換する

def my_function(x):
  return
list(dict.fromkeys(x))

mylist = my_function(["a", "b", "a", "c", "c"])

print(mylist)

リストを返す

リターンリスト

def my_function(x):
 
return list(dict.fromkeys(x))

mylist = my_function(["a", "b", "a", "c", "c"])

print(mylist)

リストをパラメーターとして使用して、関数を呼び出します。

関数を呼び出す

def my_function(x):
  return list(dict.fromkeys(x))

mylist = my_function(["a", "b", "a", "c", "c"])

print(mylist)

結果を印刷します。

結果を印刷する

def my_function(x):
  return list(dict.fromkeys(x))

mylist = my_function(["a", "b", "a", "c", "c"])

print(mylist)