logo

Patikrinkite, ar duotas raktas jau yra Python žodyne

Python žodyne negali būti pasikartojančių raktų, todėl labai svarbu patikrinti, ar raktas jau yra žodyne. Jei netyčia priskirsite pasikartojančią rakto reikšmę, nauja reikšmė perrašys senąją.

Taigi pateiktame žodyne mūsų užduotis yra patikrinti, ar duotas raktas jau yra žodyne, ar ne. Jei yra, atspausdinkite dabartį ir rakto reikšmę. Kitu atveju spausdinkite Nėra.



Pavyzdys

  Input :   {'a': 100, 'b':200, 'c':300}, key = b   Output :   Present, value = 200   Input :   {'x': 25, 'y':18, 'z':45}, key = w   Output :   Not present>

Kaip patikrinti, ar raktas jau yra žodyne

Gali būti įvairių būdų patikrinti, ar nurodytas raktas egzistuoja a Žodynas , apžvelgėme šiuos metodus:

  • Python žodyno raktai ()
  • Jei ir į
  • Python žodynas has_key()
  • Python žodynas gauti () Metodas
  • Python „KeyError“ išimčių tvarkymas
  • Python sąrašo skaičius ()

1. Patikrinkite, ar raktas egzistuoja naudojant klavišų() metodą

Keys() metodas grąžina visų galimų žodyne raktų sąrašą. Su integruotu metodu raktai(), naudokite teiginį if su operatoriumi „in“, kad patikrintumėte, ar raktas yra žodyne, ar ne.



Python3






# Python3 Program to check whether a> # given key already exists in a dictionary.> > def> checkKey(dic, key):> >if> key>in> dic.keys():> >print>(>'Present, '>, end>=>' '>)> >print>(>'value ='>, dic[key])> >else>:> >print>(>'Not present'>)> > # Driver Code> dic>=> {>'a'>:>100>,>'b'>:>200>,>'c'>:>300>}> key>=> 'b'> checkKey(dic, key)> > key>=> 'w'> checkKey(dic, key)>

>

>

Išvestis:

Present, value = 200 Not present>

Laiko sudėtingumas : O(n)
Pagalbinė erdvė : O(1)

2. Patikrinkite, ar raktas egzistuoja. Naudojant if ir in

Šis metodas naudoja jei teiginys norėdami patikrinti, ar duotas raktas yra žodyne.

Python3




def> checkKey(dic, key):> > >if> key>in> dic:> >print>(>'Present, '>, end>=>' '>)> >print>(>'value ='>, dic[key])> >else>:> >print>(>'Not present'>)> > # Driver Code> dic>=> {>'a'>:>100>,>'b'>:>200>,>'c'>:>300>}> key>=> 'b'> checkKey(dic, key)> > key>=> 'w'> checkKey(dic, key)>

>

Java rūšiavimas masyvo sąraše
>

Išvestis:

Present, value = 200 Not present>

Laiko sudėtingumas : O(n), kur n yra raktų ir reikšmių porų skaičius žodyne.
Pagalbinė erdvė : O(n), raktams ir reikšmėms išsaugoti žodyne.

3. Patikrinkite, ar raktas egzistuoja naudojant has_key() metodą

Naudojant has_key() metodas grąžina teisingą, jei duotas raktas yra žodyne, kitu atveju jis grąžina klaidingą. Naudodami integruotą metodą has_key(), naudokite teiginį if, kad patikrintumėte, ar raktas yra žodyne.

Pastaba - has_keys() metodas buvo pašalintas iš Python3 versijos. Todėl jis gali būti naudojamas tik Python2.

Python




def> checkKey(dic, key):> > >if> dic.has_key(key):> >print>(>'Present, value ='>, dic[key])> >else>:> >print>(>'Not present'>)> > # Driver Function> dic>=> {>'a'>:>100>,>'b'>:>200>,>'c'>:>300>}> key>=> 'b'> checkKey(dic, key)> > key>=> 'w'> checkKey(dic, key)>

>

>

Išvestis:

('Present, value =', 200) Not present>

4. Patikrinkite, ar raktas egzistuoja naudojant get() metodą

Integruotas metodas gauti () grąžina galimų raktų sąrašą žodyne. Naudodami keys(), naudokite if sakinį, kad patikrintumėte, ar raktas yra žodyne. Jei raktas yra, jis išspausdins Present, kitaip spausdins Not Present.

Python3




dic>=> {>'a'>:>100>,>'b'>:>200>,>'c'>:>300>}> > # check if 'b' is none or not.> if> dic.get(>'b'>)>=>=> None>:> >print>(>'Not Present'>)> else>:> >print>(>'Present'>)>

>

>

Išvestis:

Present>

5. „KeyError“ išimties tvarkymas „Python“.

Naudokite pabandyk ir išskyrus tvarkyti KeyError išimtį ir nustatyti, ar raktas yra dietoje. KeyError išimtis sugeneruojama, jei rakto, kurį bandote pasiekti, nėra žodyne.

Python3




dictExample>=> {>'Aman'>:>110>,>'Rajesh'>:>440>,>'Suraj'>:>990>}> > # Example 1> print>(>'Example 1'>)> > try>:> >dictExample[>'Kamal'>]> >print>(>'The key exists in the dictionary'>)> except> KeyError as error:> >print>(>'The key doesn't exist in the dictionary'>)> > # Example 2> print>(>'Example 2'>)> > try>:> >dictExample[>'Suraj'>]> >print>(>'The key exists in the dictionary'>)> except> KeyError as error:> >print>(>'The given key doesn't exist in the dictionary'>)>

>

>

Išvestis:

Example 1 The key doesn't exist in the dictionary Example 2 The key exists in the dictionary>

6. Patikrinkite, ar naudojamas raktas count() metodas

skaiciuoti () metodas gali būti naudojamas norint patikrinti, ar raktas yra žodyne, jei rakto skaičius yra 1, tada raktas yra kitur, jo nėra.

Python3


rūšiuoti į krūvą



# Python3 Program to check whether a> # given key already exists in a dictionary.> > # Driver Code> dic>=> {>'a'>:>100>,>'b'>:>200>,>'c'>:>300>}> key>=> 'b'> x>=> list>(dic.keys())> res>=> 'Not Present'> if>(x.count(key)>=>=> 1>):> >res>=> 'Present'> print>(res)>

>

>

Išvestis:

Present>

Šiame straipsnyje aptarėme apie 6 metodus, kuriuos galima naudoti norint patikrinti, ar žodyne yra tam tikras raktas. Norėdami patikrinti, ar yra raktas, galite naudoti bet kurį iš aukščiau pateiktų metodų. Raktų tikrinimas yra labai svarbus, nes žodyne negali būti pasikartojančių raktų.

Panašūs skaitiniai: