Pythonにおいて辞書に追加する方法はどうやるか

「イントロダクション」

辞書はPythonの組み込みデータ型です。辞書はキーと値のペアのシーケンスです。辞書は変更可能なオブジェクトですが、辞書のキーは変更不可能で、各辞書内で一意である必要があります。組み込みの追加メソッドは存在しませんが、辞書に追加や更新する方法はいくつかあります。この記事では、Pythonの代入演算子、update()メソッド、およびマージおよび更新辞書演算子を使用して、Pythonの辞書に追加および更新する方法を説明します。

Info

Silicon Cloudのアプリプラットフォームを使用して、GitHubからPythonアプリケーションを展開してください。Silicon Cloudにスケーリングに集中させてください。

=代入演算子を使用して辞書に追加

辞書に新しいキーを追加するには、「=」代入演算子を使用することができます。

dict[key] = value

もし辞書に既にキーが存在する場合は、代入演算子がその値を更新または上書きします。

以下の例は、新しい辞書を作成し、代入演算子 = を使用して値を更新し、キーと値のペアを追加する方法を示しています。

dict_example = {'a': 1, 'b': 2}

print("original dictionary: ", dict_example)

dict_example['a'] = 100  # existing key, overwrite
dict_example['c'] = 3  # new key, add
dict_example['d'] = 4  # new key, add 

print("updated dictionary: ", dict_example)

出力は:

Output

original dictionary: {‘a’: 1, ‘b’: 2} updated dictionary: {‘a’: 100, ‘b’: 2, ‘c’: 3, ‘d’: 4}

出力には、変数aの値が新しい値に上書きされ、変数bの値は変わらず、変数cとdに新しいキーバリューペアが追加されていることが表示されています。

値を上書きせずに辞書に追加する方法

=代入演算子を使用すると、既存のキーの値は新しい値で上書きされます。プログラム内に重複するキーがある可能性があるが、元の値を上書きしたくない場合は、if文を使用して条件付きで値を追加することができます。

前のセクションの例に続けて、if文を使って新しいキーと値の組みを辞書に追加することができます。

dict_example = {'a': 1, 'b': 2}

print("original dictionary: ", dict_example)

dict_example['a'] = 100  # existing key, overwrite
dict_example['c'] = 3  # new key, add
dict_example['d'] = 4  # new key, add 

print("updated dictionary: ", dict_example)

# add the following if statements

if 'c' not in dict_example.keys():
    dict_example['c'] = 300

if 'e' not in dict_example.keys():
    dict_example['e'] = 5

print("conditionally updated dictionary: ", dict_example)

出力は:

Output

original dictionary: {‘a’: 1, ‘b’: 2} updated dictionary: {‘a’: 100, ‘b’: 2, ‘c’: 3, ‘d’: 4} conditionally updated dictionary: {‘a’: 100, ‘b’: 2, ‘c’: 3, ‘d’: 4, ‘e’: 5}

出力は、if条件のため、辞書が条件付きで更新されたとしても、cの値は変更されなかったことを示しています。

update() メソッドを使って辞書に追加する。

update()メソッドを使用して、辞書を追加するか、キーと値の組のイテラブルを辞書に追加することができます。update()メソッドは、既存のキーの値を新しい値で上書きします。

以下の例は、新しい辞書を作成し、update()メソッドを使用して新しいキーと値のペア、および新しい辞書を追加し、結果をそれぞれ印刷する方法を示しています。

site = {'Website':'Silicon Cloud', 'Tutorial':'How To Add to a Python Dictionary'}
print("original dictionary: ", site)

# update the dictionary with the author key-value pair
site.update({'Author':'Sammy Shark'})
print("updated with Author: ", site)

# create a new dictionary
guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}

# update the original dictionary with the new dictionary
site.update(guests)
print("updated with new dictionary: ", site)

出力は:

Output

original dictionary: {‘Website’: ‘Silicon Cloud’, ‘Tutorial’: ‘How To Add to a Python Dictionary’} updated with Author: {‘Website’: ‘Silicon Cloud’, ‘Tutorial’: ‘How To Add to a Python Dictionary’, ‘Author’: ‘Sammy Shark’} updated with new dictionary: {‘Website’: ‘Silicon Cloud’, ‘Tutorial’: ‘How To Add to a Python Dictionary’, ‘Author’: ‘Sammy Shark’, ‘Guest1’: ‘Dino Sammy’, ‘Guest2’: ‘Xray Sammy’}

出力結果によると、最初の更新では新しいキーと値のペアが追加され、二つ目の更新ではゲスト辞書からサイト辞書にキーと値のペアが追加されます。ただし、辞書の更新に既存のキーが含まれている場合、古い値は更新によって上書きされることに注意してください。

「Merge」演算子を使用して辞書に追加する方法

「パイプ」文字で表される辞書のマージには、2つの辞書を結合して新しい辞書オブジェクトを返す「マージ | 演算子」を使用できます。

以下の例は、2つの辞書を作成し、マージ演算子を使用して両方の辞書のキーと値のペアを含む新しい辞書を作成する方法を示しています。

site = {'Website':'Silicon Cloud', 'Tutorial':'How To Add to a Python Dictionary', 'Author':'Sammy'}

guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}

new_site = site | guests

print("site: ", site)
print("guests: ", guests)
print("new_site: ", new_site)

出力は以下の通りです。

Output

site: {‘Website’: ‘Silicon Cloud’, ‘Tutorial’: ‘How To Add to a Python Dictionary’, ‘Author’: ‘Sammy’} guests: {‘Guest1’: ‘Dino Sammy’, ‘Guest2’: ‘Xray Sammy’} new_site: {‘Website’: ‘Silicon Cloud’, ‘Tutorial’: ‘How To Add to a Python Dictionary’, ‘Author’: ‘Sammy’, ‘Guest1’: ‘Dino Sammy’, ‘Guest2’: ‘Xray Sammy’}

2つの辞書は新しい辞書オブジェクトに統合され、両方の辞書からキーと値のペアが含まれています。

もし2つの辞書に共通のキーが存在する場合、2番目の辞書または右のオペランドからの値が取得されます。以下の例のコードでは、両方の辞書にbというキーがあります。

dict1 = {'a':'one', 'b':'two'}
dict2 = {'b':'letter two', 'c':'letter three'}

dict3 = dict1 | dict2

print{"dict3: ", dict3}

出力は次のようになります。

Output

dict3: {‘a’: ‘one’, ‘b’: ‘letter two’, ‘c’: ‘letter three’}

辞書の右側のオペランド、dict2からの値によって、キーbの値が上書きされました。

“Update |=演算子を使用して辞書に追加する”

与えられた辞書や値を使って、辞書をその場で更新するために、パイプと等号の記号で表される辞書の更新 |= 演算子を使用することができます。

マージ | 演算子と同様に、もし両方の辞書にキーが存在する場合、更新する |= 演算子は右オペランドから値を取ります。

以下の例は、2つの辞書を作成し、更新演算子を使用して第2の辞書を第1の辞書に追加し、その後更新された辞書を表示する方法を示しています。

site = {'Website':'Silicon Cloud', 'Tutorial':'How To Add to a Python Dictionary', 'Author':'Sammy'}

guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}

site |= guests

print("site: ", site)

出力は:

Output

site: {‘Website’: ‘Silicon Cloud’, ‘Tutorial’: ‘How To Add to a Python Dictionary’, ‘Author’: ‘Sammy’, ‘Guest1’: ‘Dino Sammy’, ‘Guest2’: ‘Xray Sammy’}

前の例では、元のオブジェクトが変更されるため、サードの辞書オブジェクトを作成する必要はありませんでした。出力は、ゲストの辞書が元のサイトの辞書に追加されたことを示しています。

結論:

結論とは、最終的な結果や結末を指すものです。

この記事では、Pythonの辞書に追加したり更新するためのさまざまな方法を使用しました。さらにPythonのチュートリアルを通じて学習を続けてください。

コメントを残す 0

Your email address will not be published. Required fields are marked *