Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet159/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   155   156   157   158   159   160   161   162   ...   183
Bog'liq
python3-oop

[
 164 
]
In contrast, there are no limits on the types of objects that can be used as dictionary 
values. We can use a string key that maps to a list value, for example, or we can
have a nested dictionary as a value in another dictionary.
Dictionary use cases
Dictionaries are extremely versatile and have numerous uses. There are two major 
ways that dictionaries can be used. The first is dictionaries where all the keys 
represent different instances of similar objects; for example, our stock dictionary. 
This is an indexing system. We use the stock symbol as an index to the values.
The values could even have been complicated self-defined objects that made buy 
and sell decisions or set a stop-loss, rather than our simple tuples.
The second design is dictionaries where each key represents some aspect of a single 
structure; in this case, we'd probably use a separate dictionary for each object, and 
they'd all have similar (though often not identical) sets of keys. This latter situation 
can often also be solved with named tuples. These should typically be used when 
we know exactly what attributes the data must store, and we know that all pieces of 
the data must be supplied at once (when the item is constructed). But if we need to 
create or change dictionary keys over time or we don't know exactly what the keys 
might be, a dictionary is more suitable.
Using defaultdict
We've seen how to use 
setdefault
to set a default value if a key doesn't exist, but 
this can get a bit monotonous if we need to set a default value every time we look
up a value. For example, if we're writing code that counts the number of times a 
letter occurs in a given sentence, we could do this:
def letter_frequency(sentence):
frequencies = {}
for letter in sentence:
frequency = frequencies.setdefault(letter, 0)
frequencies[letter] = frequency + 1
return frequencies
Every time we access the dictionary, we need to check that it has a value already, 
and if not, set it to zero. When something like this needs to be done every time an 
empty key is requested, we can use a different version of the dictionary, called 
defaultdict
:

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   155   156   157   158   159   160   161   162   ...   183




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish