Slovar se uporablja tudi v številnih praktičnih aplikacijah, kot so dnevno programiranje, spletni razvoj in programiranje AI/ML, zaradi česar je na splošno uporaben vsebnik. Zato je poznavanje stenografij za doseganje različnih nalog, povezanih z uporabo slovarja, vedno plus. Ta članek obravnava eno tako nalogo brisanja/odstranjevanja slovarskega para ključ-vrednost iz slovarja, razpravljali bomo o različnih metodah za obravnavo dane naloge, v zadnjem delu pa bomo videli, kako lahko izbrišemo vse ključe iz Slovar .
primer:
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. način: Odstranite ključ iz slovarja z uporabo del
Ključno besedo del lahko uporabite za brisanje ključa na mestu, ki je prisoten v slovarju v Pythonu. Ena od pomanjkljivosti, ki si jo lahko predstavljamo pri uporabi tega, je, da sproži izjemo, če ključa ni mogoče najti, zato je treba obravnavati neobstoj ključa. Prikaz brisanja para ključ-vrednost z 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'> ]> |
>
>
spanje v javascriptu
Izhod:
The dictionary before performing remove is : {'Arushi': 22, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Haritha': 21}>
Izjema:
Traceback (most recent call last): File '/home/44db951e7011423359af4861d475458a.py', line 20, in del test_dict['Mani'] KeyError: 'Mani'>
Časovna zahtevnost inicializacije slovarja in odstranitve postavke iz slovarja z uporabo stavka del je O(1).
Pomožni prostor, potreben za to kodo, je O(1), saj le spreminjamo obstoječi slovar in ne ustvarjamo novih podatkovnih struktur.
2. način: odstranite ključ iz slovarja s pop()
The pop() lahko uporabite za brisanje ključa in njegove vrednosti na mestu. Prednost pred uporabo del je, da zagotavlja mehanizem za tiskanje želene vrednosti, če poskušate odstraniti neobstoječi dikt. par. Drugič, poleg izvedbe preproste operacije brisanja vrne tudi vrednost ključa, ki je bil odstranjen. Prikaz brisanja para ključ-vrednost z uporabo 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))> |
>
>
Izhod:
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>
Časovna zahtevnost: O(1)
Pomožni prostor: O(1)
3. način: Uporaba items() + dict comprehension za odstranitev ključa iz slovarja
items (), skupaj z razumevanjem nareka, nam lahko prav tako pomaga doseči nalogo brisanja para ključ-vrednost, vendar ima to pomanjkljivost, da ni narek na mestu. tehnika. Pravzaprav je ustvarjen nov dikt razen ključa, ki ga ne želimo vključiti. Prikaz brisanja para ključ-vrednost z uporabo items() + dict razumevanje.
spremenljivka bash
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))> |
>
>
Izhod:
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}>
Časovna kompleksnost: O(n), kjer je n dolžina seznama test_dict
Pomožni prostor: Ustvari se O(n) dodatni prostor velikosti n, kjer je n število elementov na seznamu res
4. način: uporabite razumevanje slovarja Python za odstranitev ključa iz slovarja
V tem primeru bomo uporabili slovarsko razumevanje za odstranitev ključa iz slovarja.
Python3
q1 q2 q3 q4
# 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)> |
>
>
Izhod:
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. način: ponavljanje in izločanje
V tem primeru bomo uporabili a zanka za odstranitev ključa iz slovarja.
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)> |
>
>
Izhod:
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} {'Anuradha': 21, 'Mani': 21, 'Haritha': 21}>
Kako izbrisati vse ključe iz slovarja?
1. način: Izbrišite vse ključe iz slovarja z uporabo del
Ključno besedo del lahko uporabite tudi za brisanje seznama, rezanje seznama, brisanje slovarjev, odstranjevanje parov ključ-vrednost iz slovarja, brisanje spremenljivk itd.
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!'> )> |
>
>
preverjanje ničelne vrednosti java
Izhod:
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} Deleted!>
2. način: Izbrišite vse ključe iz slovarja z dict.clear()
Metoda clear() odstrani vse elemente iz slovarja. Metoda clear() ne vrne nobene vrednosti.
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)> |
>
>
Izhod:
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} Length 0 {}>
Časovna zahtevnost: O(1)
Pomožni prostor: O(1)