Žodynas naudojamas įvairiose praktinėse programose, tokiose kaip dienos programavimas, interneto svetainių kūrimas ir AI / ML programavimas, todėl jis yra naudingas konteineris. Taigi, žinant trumpuosius žodžius, kaip atlikti įvairias su žodyno naudojimu susijusias užduotis, visada yra privalumas. Šiame straipsnyje aptariama viena iš tokių užduočių – ištrinti / pašalinti žodyno raktų ir reikšmių porą iš žodyno, aptarsime skirtingus metodus, kaip atlikti tam tikrą užduotį, o Paskutiniame skyriuje pamatysime, kaip galime ištrinti visus raktus iš žodyno. Žodynas .
Pavyzdys:
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 būdas: pašalinkite raktą iš žodyno naudodami del
Raktinis žodis del gali būti naudojamas norint vietoje ištrinti raktą, esantį Python žodyne. Vienas trūkumas, kurį galima manyti naudojant šį metodą, yra tas, kad, jei raktas nerandamas, atsiranda išimtis, todėl reikia tvarkyti rakto nebuvimą. Raktų ir verčių poros ištrynimo demonstravimas naudojant 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'> ]> |
>
>
nuo 1 iki 100 romėnų Nr
Išvestis:
The dictionary before performing remove is : {'Arushi': 22, 'Mani': 21, 'Haritha': 21} The dictionary after remove is : {'Arushi': 22, 'Haritha': 21}>
Išimtis:
Traceback (most recent call last): File '/home/44db951e7011423359af4861d475458a.py', line 20, in del test_dict['Mani'] KeyError: 'Mani'>
Žodyno inicijavimo ir elemento pašalinimo iš žodyno naudojant del teiginį sudėtingumas yra O(1).
Šiam kodui reikalinga pagalbinė erdvė yra O(1), nes mes tik modifikuojame esamą žodyną ir nekuriame jokių naujų duomenų struktūrų.
2 būdas: pašalinkite raktą iš žodyno naudodami pop()
The pop () gali būti naudojamas norint ištrinti raktą ir jo reikšmę vietoje. Privalumas, palyginti su del naudojimu, yra tas, kad jis suteikia mechanizmą norimai vertei spausdinti, jei bandoma pašalinti neegzistuojantį diktatą. pora. Antra, ji taip pat grąžina rakto, kuris yra pašalinamas, vertę, be paprastos ištrynimo operacijos. Rakto ir verčių poros ištrynimo demonstravimas naudojant 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))> |
>
>
Išvestis:
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>
Laiko sudėtingumas: O(1)
Pagalbinė erdvė: O(1)
3 būdas: elementų () + diktavimo supratimo naudojimas norint pašalinti raktą iš žodyno
elementai () kartu su dikto supratimu taip pat gali padėti mums pasiekti raktų ir reikšmių porų ištrynimo užduotį, tačiau jis turi trūkumą, nes tai nėra diktas vietoje. technika. Tiesą sakant, sukuriamas naujas diktas, išskyrus raktą, kurio nenorime įtraukti. Raktų ir reikšmių poros ištrynimo demonstravimas naudojant elementus () + dikto supratimas.
Python3
Java polimorfizmas
# 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))> |
>
>
Išvestis:
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}>
Laiko sudėtingumas: O(n), kur n yra sąrašo test_dict ilgis
Pagalbinė erdvė: sukuriama O(n) papildoma n dydžio erdvė, kur n yra elementų skaičius res sąraše
4 būdas: naudokite Python žodyno supratimą, kad pašalintumėte raktą iš žodyno
Šiame pavyzdyje mes naudosime žodyno supratimą, kad pašalintume raktą iš žodyno.
Python3
java rūšiavimo masyvas
# 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)> |
>
>
Išvestis:
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 būdas: kartojimas ir pašalinimas
Šiame pavyzdyje naudosime a kilpa norėdami pašalinti raktą iš žodyno.
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)> |
>
>
Išvestis:
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} {'Anuradha': 21, 'Mani': 21, 'Haritha': 21}>
Kaip ištrinti visus raktus iš žodyno?
1 būdas: ištrinkite visus raktus iš žodyno naudodami del
Raktinis žodis del taip pat gali būti naudojamas sąrašui ištrinti, sąrašui pjaustyti, žodynams ištrinti, raktų ir reikšmių poras iš žodyno, kintamiesiems ištrinti ir kt.
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!'> )> |
>
>
Išvestis:
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} Deleted!>
2 būdas: ištrinkite visus raktus iš žodyno naudodami dict.clear()
Clear() metodas pašalina visus elementus iš žodyno. Clear () metodas nepateikia jokios reikšmės.
eilutė pakeisti visą java
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)> |
>
>
Išvestis:
{'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21} Length 0 {}>
Laiko sudėtingumas: O(1)
Pagalbinė erdvė: O(1)