Pridėkite rakto:vertės porą į žodyną programoje Python
Žodynas Python yra netvarkingas duomenų reikšmių rinkinys, naudojamas duomenų reikšmėms, pvz., žemėlapiui, saugoti. Skirtingai nuo kitų duomenų tipų, kurių elementas turi tik vieną reikšmę, žodyne yra rakto: vertės pora. Naudodami žodyną kartais turime pridėti arba pakeisti raktą / reikšmę žodyne. Pažiūrėkime, kaip į Python žodyną įtraukti rakto:vertės porą.
1 kodas: Subscription naudojimas Šis metodas sukurs naują rakto:reikšmės porą žodyne, priskirdamas tam raktui reikšmę.
Python3
# Python program to add a key:value pair to dictionary> dict> => {> 'key1'> :> 'geeks'> ,> 'key2'> :> 'for'> }> print> ('Current> Dict> is> : ',> dict> )> > # using the subscript notation> # Dictionary_Name[New_Key_Name] = New_Key_Value> dict> [> 'key3'> ]> => 'Geeks'> dict> [> 'key4'> ]> => 'is'> dict> [> 'key5'> ]> => 'portal'> dict> [> 'key6'> ]> => 'Computer'> print> ('Updated> Dict> is> : ',> dict> )> |
Išvestis:
Dabartinis diktatas yra: {'key2': 'for', 'key1': 'geeks'} Atnaujintas diktas yra: {'key3': 'Geeks', 'key5': 'portalas', 'key6': 'Kompiuteris', „key4“: „yra“, „key1“: „geeks“, „key2“: „už“}
Laiko sudėtingumas: O(1)
Pagalbinė erdvė: O(1)
Kodas #2: Naudojant update() metodą
Python3
dict> => {> 'key1'> :> 'geeks'> ,> 'key2'> :> 'for'> }> print> ('Current> Dict> is> : ',> dict> )> # adding dict1 (key3, key4 and key5) to dict> dict1> => {> 'key3'> :> 'geeks'> ,> 'key4'> :> 'is'> ,> 'key5'> :> 'fabulous'> }> dict> .update(dict1)> # by assigning> dict> .update(newkey1> => 'portal'> )> print> (> dict> )> |
Išvestis:
Dabartinis diktatas yra: {'key2': 'for', 'key1': 'geeks'} {'newkey1': 'portalas', 'key4': 'is', 'key2': 'for', 'key1': 'geeks', 'key5': 'fabulous', 'key3': 'geeks'}
Laiko sudėtingumas: O(1)
Pagalbinė erdvė: O(1)
Kodas #3: Kaip įvestį imamas raktas: vertė
Python3
# Let's add key:value to a dictionary, the functional way> # Create your dictionary class> class> my_dictionary(> dict> ):> > # __init__ function> > def> __init__(> self> ):> > self> => dict> ()> > > # Function to add key:value> > def> add(> self> , key, value):> > self> [key]> => value> # Main Function> dict_obj> => my_dictionary()> # Taking input key = 1, value = Geek> dict_obj.key> => input> ('Enter the key: ')> dict_obj.value> => input> ('Enter the value: ')> dict_obj.add(dict_obj.key, dict_obj.value)> dict_obj.add(> 2> ,> 'forGeeks'> )> print> (dict_obj)> |
Išvestis:
{'1': 'Geeks', 2: 'forGeeks'} Laiko sudėtingumas: O(1)
Pagalbinė erdvė: O(n)
Kodas #4: Naudojant žodyno supratimą
Pavyzdžiui, galite sukurti naują žodyną, kuris į esamą žodyną prideda porą raktas:reikšmė, pvz.:
Python3
existing_dict> => {> 'key1'> :> 'value1'> ,> 'key2'> :> 'value2'> }> new_key> => 'key3'> new_value> => 'value3'> updated_dict> => {> *> *> existing_dict, new_key: new_value}> print> (updated_dict)> #This code is contributed by Edula Vinay Kumar Reddy> |
Išvestis
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'} Taip sukuriamas naujas žodynas, pavadintas updated_dict, kuriame yra visos rakto:reikšmės poros iš esamo_dikto, taip pat nauja raktas:reikšmė pora „key3“: „value3“.
Laiko sudėtingumas: O(n)
Pagalbinė erdvė: O(n)