Dictionary は、日常的なプログラミング、Web 開発、AI/ML プログラミングなどのさまざまな実用的なアプリケーションでも使用されており、全体的に有用なコンテナーとなっています。したがって、辞書の使用に関連するさまざまなタスクを実行するための略記法を知っていると、常にプラスになります。この記事では、辞書のキーと値のペアを辞書から削除/削除するタスクの 1 つを扱います。与えられたタスクに対処するためのさまざまな方法について説明します。最後に、辞書からすべてのキーを削除する方法を説明します。 辞書 。
例:
Before remove key: {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21 } Operation Perform: del test_dict['Mani'] After removing key: {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}> 方法 1: del を使用して辞書からキーを削除する
del キーワードを使用すると、Python の辞書に存在するキーをインプレースで削除できます。これを使用すると考えられる欠点の 1 つは、キーが見つからない場合に例外が発生するため、キーが存在しないことを処理する必要があることです。 del を使用したキーと値のペアの削除のデモ。
Python3
# Initializing dictionary> test_dict>=> {>'Arushi'>:>22>,>'Mani'>:>21>,>'Haritha'>:>21>}> # Printing dictionary before removal> print>(>'The dictionary before performing remove is : '>, test_dict)> # Using del to remove a dict> # removes Mani> del> test_dict[>'Mani'>]> # Printing dictionary after removal> print>(>'The dictionary after remove is : '>, test_dict)> # Using del to remove a dict> # raises exception> del> test_dict[>'Mani'>]> |
>
>
ソフトウェアテストにおける回帰テスト
出力:
The dictionary before performing remove is : {'Arushi': 22, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Haritha': 21}> 例外 :
Traceback (most recent call last): File '/home/44db951e7011423359af4861d475458a.py', line 20, in del test_dict['Mani'] KeyError: 'Mani'>
del ステートメントを使用して辞書を初期化し、辞書から項目を削除する時間計算量は O(1) です。
既存のディクショナリを変更するだけで新しいデータ構造は作成しないため、このコードに必要な補助スペースは O(1) です。
方法 2: Pop() を使用して辞書からキーを削除する
の ポップ() キーとその値をインプレースで削除するために使用できます。 del を使用する場合と比べた利点は、存在しない辞書を削除しようとした場合に、必要な値を出力するメカニズムが提供されることです。ペア。次に、単純な削除操作の実行に加えて、削除されるキーの値も返します。 Pop() を使用したキーと値のペアの削除のデモ
Python3
# Initializing dictionary> test_dict>=> {>'Arushi'>:>22>,>'Anuradha'>:>21>,>'Mani'>:>21>,>'Haritha'>:>21>}> # Printing dictionary before removal> print>(>'The dictionary before performing remove is : '> +> str>(test_dict))> # Using pop() to remove a dict. pair> # removes Mani> removed_value>=> test_dict.pop(>'Mani'>)> # Printing dictionary after removal> print>(>'The dictionary after remove is : '> +> str>(test_dict))> print>(>'The removed key's value is : '> +> str>(removed_value))> print>(>'
'>)> # Using pop() to remove a dict. pair> # doesn't raise exception> # assigns 'No Key found' to removed_value> removed_value>=> test_dict.pop(>'Manjeet'>,>'No Key found'>)> # Printing dictionary after removal> print>(>'The dictionary after remove is : '> +> str>(test_dict))> print>(>'The removed key's value is : '> +> str>(removed_value))> |
>
gimpでjpgとしてエクスポート
>
出力:
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : 21 The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21} The removed key's value is : No Key found> 時間計算量: O(1)
補助スペース: O(1)
方法 3: items() + dict 内包表記を使用して辞書からキーを削除する
items() を dict 内包と組み合わせると、キーと値のペアを削除するタスクを達成するのに役立ちますが、インプレース dict ではないという欠点があります。技術。実際には、含めたくないキーを除いて、新しい辞書が作成されます。 items() + dict 内包表記を使用したキーと値のペアの削除を示します。
Python3
# Initializing dictionary> test_dict>=> {>'Arushi'>:>22>,>'Anuradha'>:>21>,> >'Mani'>:>21>,>'Haritha'>:>21>}> # Printing dictionary before removal> print>('The dictionary before performing> remove>is> : '>+> str>(test_dict))> # Using items() + dict comprehension to remove a dict. pair> # removes Mani> new_dict>=> {key: val>for> key,> >val>in> test_dict.items()>if> key !>=> 'Mani'>}> # Printing dictionary after removal> print>(>'The dictionary after remove is : '> +> str>(new_dict))> |
>
>
出力:
The dictionary before performing remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21} The dictionary after remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}> 時間計算量: O(n)、n はリスト test_dict の長さです
補助スペース: サイズ n の O(n) 個の追加スペースが作成されます。ここで、n は res リスト内の要素の数です。
方法 4: Python 辞書内包表記を使用して辞書からキーを削除する
この例では、Dictionary Comprehension を使用して辞書からキーを削除します。
Python3
# Initializing dictionary> test_dict>=> {>'Arushi'>:>22>,>'Anuradha'>:>21>,>'Mani'>:>21>,>'Haritha'>:>21>}> # Printing dictionary before removal> print>(>'The dictionary before performing remove is :
'> +> str>(test_dict))> a_dict>=> {key: test_dict[key]>for> key>in> test_dict>if> key !>=> 'Mani'>}> print>(>'The dictionary after performing remove is :
'>, a_dict)> |
>
>
出力:
The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} The dictionary after performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}> 方法 5: 反復と削除
この例では、 ループ 辞書からキーを削除します。
Python3
# Initializing dictionary> test_dict>=> {>'Arushi'>:>22>,>'Anuradha'>:>21>,>'Mani'>:>21>,>'Haritha'>:>21>}> print>(test_dict)> # empty the dictionary d> y>=> {}> # eliminate the unrequired element> for> key, value>in> test_dict.items():> >if> key !>=> 'Arushi'>:> >y[key]>=> value> print>(y)> |
>
>
出力:
あなたはスプライスです
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} {'Anuradha': 21, 'Mani': 21, 'Haritha': 21}> 辞書からすべてのキーを削除するにはどうすればよいですか?
方法 1: del を使用して辞書からすべてのキーを削除する
del キーワードは、リストの削除、リストのスライス、辞書の削除、辞書からのキーと値のペアの削除、変数の削除などにも使用できます。
Python3
# Initializing dictionary> test_dict>=> {>'Arushi'>:>22>,>'Anuradha'>:>21>,>'Mani'>:>21>,>'Haritha'>:>21>}> print>(test_dict)> # empty the dictionary d> del> test_dict> try>:> >print>(test_dict)> except>:> >print>(>'Deleted!'>)> |
>
>
マイリケット
出力:
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} Deleted!> 方法 2: dict.clear() を使用して辞書からすべてのキーを削除する
clear() メソッドは、辞書からすべての項目を削除します。 clear() メソッドは値を返しません。
Python3
# Initializing dictionary> test_dict>=> {>'Arushi'>:>22>,>'Anuradha'>:>21>,>'Mani'>:>21>,>'Haritha'>:>21>}> print>(test_dict)> # empty the dictionary d> test_dict.clear()> print>(>'Length'>,>len>(test_dict))> print>(test_dict)> |
>
>
出力:
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} Length 0 {}> 時間計算量: ○(1)
補助スペース: ○(1)