Mètode d'actualització del diccionari Python ().

Mètode d'actualització del diccionari Python (). actualitza el diccionari amb els elements d'un altre objecte diccionari o d'un iterable de parells clau/valor.

Exemple:

  Original dictionary :   {'A': 'Geeks', 'B': 'For'}   Updated dictionary :   {'A': 'Geeks', 'B': 'Geeks'}    Original dictionary :   {'A': 'Geeks', 'B': 'For'}   Updated dictionary :   {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 

Sintaxi del mètode d'actualització del diccionari Python

El mètode update() del diccionari a Python té la sintaxi següent:

Sintaxi: dict.update([altre])

Paràmetres: Aquest mètode pren com a paràmetres un diccionari o un objecte iterable de parells clau/valor (generalment tuples).

Devolucions: No retorna cap valor, però actualitza el Diccionari amb elements d'un objecte diccionari o un objecte iterable de parells clau/valor.

Actualització del diccionari Python() Exemple

Vegem uns quants exemples del mètode update() per actualitzar les dades del fitxer Diccionari Python .

Actualitzar amb un altre diccionari

Aquí estem actualitzant un diccionari en Python mitjançant el mètode update() i passant-li un altre diccionari com a paràmetres. El segon diccionari s'utilitza per al valor actualitzat.

Python 3




# Python program to show working> # of update() method in Dictionary> # Dictionary with three items> Dictionary1> => {> 'A'> :> 'Geeks'> ,> 'B'> :> 'For'> , }> Dictionary2> => {> 'B'> :> 'Geeks'> }> # Dictionary before Updation> print> (> 'Original Dictionary:'> )> print> (Dictionary1)> # update the value of key 'B'> Dictionary1.update(Dictionary2)> print> (> 'Dictionary after updation:'> )> print> (Dictionary1)>

Sortida:

Original Dictionary: {'A': 'Geeks', 'B': 'For'} Dictionary after updation: {'A': 'Geeks', 'B': 'Geeks'} 

Actualitzar amb un iterable

En aquest exemple, en lloc d'utilitzar un altre diccionari, hem passat un valor iterable a la funció update().

Python 3




# Python program to show working> # of update() method in Dictionary> # Dictionary with single item> Dictionary1> => {> 'A'> :> 'Geeks'> }> # Dictionary before Updation> print> (> 'Original Dictionary:'> )> print> (Dictionary1)> # update the Dictionary with iterable> Dictionary1.update(B> => 'For'> , C> => 'Geeks'> )> print> (> 'Dictionary after updation:'> )> print> (Dictionary1)>

Sortida

Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} 

El diccionari Python Actualitza el valor si la clau existeix

En aquest exemple, actualitzarem el valor d'un diccionari a Python si la clau concreta existeix. Si la clau no està present al diccionari, simplement imprimirem que la clau no existeix.

Python 3




def> checkKey(> dict> , key):> > > if> key> in> dict> .keys():> > print> (> 'Key exist, '> , end> => ' '> )> > dict> .update({> 'm'> :> 600> })> > print> (> 'value updated ='> ,> 600> )> > else> :> > print> (> 'Not Exist'> )> dict> => {> 'm'> :> 700> ,> 'n'> :> 100> ,> 't'> :> 500> }> > key> => 'm'> checkKey(> dict> , key)> print> (> dict> )>

Sortida:

Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} 

Valor d'actualització del diccionari Python si la clau no existeix

Aquí, intentarem actualitzar el valor del diccionari la clau del qual no existeix al diccionari. En aquest cas, la clau i el valor s'afegiran com a element nou al diccionari.

Python 3




def> checkKey(> dict> , key):> > > if> key> not> in> dict> .keys():> > print> (> 'Key doesn't exist So, a new Key-Value pair will be created'> )> > dict> .update({key:> 600> })> > else> :> > print> (> 'Key Exist'> )> dict> => {> 'm'> :> 700> ,> 'n'> :> 100> ,> 't'> :> 500> }> > key> => 'k'> checkKey(> dict> , key)> print> (> dict> )>

Sortida:

Key doesn't exist So, a new Key-Value pair will be created {'m': 700, 'n': 100, 't': 500, 'k': 600}