To’plamlar haqida ma’lumot. II. Asosiy qism
defaultdict dictdan qanday farq qiladi
Download 136.13 Kb.
|
d.dasturlash
- Bu sahifa navigatsiya:
- OrderedDict
defaultdict dictdan qanday farq qiladiAgar siz lug'atda mavjud bo'lmagan kalitga kirishga harakat qilsangiz, u KeyError. Holbuki, a defaultdictda u ni bermaydi KeyError. Bu kalit xatosini bermaydi. Agar mavjud bo'lmagan kalitga kirsangiz, defaultdictu standart qiymatni qaytaradi. Sintaksis:defaultdict(default_factory) Biz mavjud bo'lmagan kalitga kirganimizda, default_factoryfunktsiya standart qiymatni qaytaradi # Creating a defaultdict and trying to access a key that is not present. from collections import defaultdict def_dict = defaultdict(object) def_dict['fruit'] = 'orange' def_dict['drink'] = 'pepsi' print(def_dict['chocolate']) #> <object object at 0x7f591a2f4510> Yuqoridagi buyruqni bajarsangiz, u sizga bermaydi KeyError. Agar siz so'ralgan kalit uchun qiymat mavjud emasligini chiqarmoqchi bo'lsangiz, o'zingizning funksiyangizni belgilashingiz va uni defaultdict-ga o'tkazishingiz mumkin. Quyidagi misolga qarang # Passing a function to return default value def print_default(): return 'value absent' def_dict=defaultdict(print_default) print(def_dict['chocolate']) #> value absent Boshqa barcha yo'llar bilan u oddiy lug'at bilan bir xil. Xuddi shu sintaksis buyruqlari defaultdict uchun ham ishlatiladi. Aslida, usul KeyErroryordamida lug'atni engib o'tish mumkin.get # Make dict return a default value mydict = {'a': 'Apple', 'b': 'Ball'} mydict.get('c', 'NOT PRESENT') #> 'NOT PRESENT' OrderedDictDict - bu kalit qiymat juftlarining TARTIB ETMAGAN to'plami. Biroq, OrderedDictkalitlar kiritilgan TARTIBIni saqlaydi. ning kichik sinfidir dict. Men oddiy yaratmoqchiman dictva uni OrderedDictsizga farqni ko'rsatish uchun qilaman # create a dict and print items vehicle = {'bicycle': 'hercules', 'car': 'Maruti', 'bike': ' Harley', 'scooter': 'bajaj'} print('This is normal dict') for key,value in vehicle.items(): print(key,value) print('-------------------------------') # Create an OrderedDict and print items from collections import OrderedDict ordered_vehicle=OrderedDict() ordered_vehicle['bicycle']='hercules' ordered_vehicle['car']='Maruti' ordered_vehicle['bike']='Harley' print('This is an ordered dict') for key,value in ordered_vehicle.items(): print(key,value) #> This is normal dict #> bicycle hercules #> car Maruti #> bike Harley #> scooter bajaj ------------------------------- #> This is an ordered dict #> bicycle hercules #> car Maruti #> bike Harley In OrderedDict, hatto ba'zi kalitlarning qiymatini o'zgartirgandan keyin ham, tartib bir xil yoki o'zgarishsiz qoladi. # I have changed the value of car in this ordered dictionary. ordered_vehicle['car']='BMW'# I have changed the value of car in this ordered dictionary. for key,value in ordered_vehicle.items(): print(key,value) #> bicycle hercules #> car BMW #> bike harley davison Download 136.13 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling