Author Dusty Phillips Reviewers


from collections import defaultdict



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

from collections import defaultdict
def letter_frequency(sentence):
frequencies = defaultdict(int)
www.it-ebooks.info


Chapter 6
[
 165 
]
for letter in sentence:
frequencies[letter] += 1
return frequencies
This code looks like it couldn't possibly work. The 
defaultdict
accepts a function in 
its constructor. Whenever a key is accessed that is not already in the dictionary,
it calls that function, with no parameters, to create a default value.
In this case, the function it calls is 
int
, which is the constructor for an integer object. 
Normally, integers are created simply by typing an integer number into our code, and 
if we do create one using the 
int
constructor, we pass it the item we want to create (for 
example, to convert a string of digits into an integer). But if we call 
int
without any 
arguments, it returns, conveniently, the number zero. In this code, if the letter doesn't 
exist in the 
defaultdict
, the number zero is returned when we access it. Then we add 
one to this number to indicate we've found an instance of that letter, and the next time 
we find one, that number will be returned and we can increment the value again.
The 
defaultdict
is useful for creating dictionaries of containers. If we want to 
create a dictionary of stock prices for the past 30 days, we could use a stock symbol 
as the key and store the prices in 
list
; the first time we access the stock price, we 
would want it to create an empty list. Simply pass 
list
into the 
defaultdict
, and 
it will be called every time an empty key is accessed. We can do similar things with 
sets or even empty dictionaries if we want to associate one with a key.
Of course, we can also write our own functions and pass them into the 
defaultdict

Suppose we want to create a 
defaultdict
where each new element contains a tuple 
of the number of items inserted into the dictionary at that time and an empty list to 
hold other things. Nobody knows why we would want to create such an object, but 
let's have a look:
from collections import defaultdict
num_items = 0
def tuple_counter():
global num_items
num_items += 1
return (num_items, [])

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   156   157   158   159   160   161   162   163   ...   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