Author Dusty Phillips Reviewers


d = defaultdict(tuple_counter)



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

d = defaultdict(tuple_counter)
When we run this code, we can access empty keys and insert into the list all in
one statement:
>>> d = defaultdict(tuple_counter)
>>> d['a'][1].append("hello")
>>> d['b'][1].append('world')
www.it-ebooks.info


Python Data Structures
[
 166 
]
>>> d
defaultdict(,
{'a': (1, ['hello']), 'b': (2, ['world'])})
When we print 
dict
at the end, we see that the counter really was working.
This example, while succinctly demonstrating how to create our own 
function for 
defaultdict
, is not actually very good code; using a 
global variable means that if we created four different 
defaultdict
segments that each used 
tuple_counter
, it would count the number 
of entries in all dictionaries, rather than having a different count for 
each one. It would be better to create a class and pass a method on that 
class to 
defaultdict
.
Counter
You'd think that you couldn't get much simpler than 
defaultdict(int)
, but the 
"I want to count specific instances in an iterable" use case is common enough that 
the Python developers created a specific class for it. The previous code that counts 
characters in a string can easily be calculated in a single line:
from collections import Counter
def letter_frequency(sentence):
return Counter(sentence)
The 
Counter
object behaves like a beefed up dictionary where the keys are the items 
being counted and the values are the number of such items. One of the most useful 
functions is the 
most_common()
method. It returns a list of (key, count) tuples ordered 
by the count. You can optionally pass an integer argument into 
most_common()
to 
request only the top most common elements. For example, you could write a simple 
polling application as follows:
from collections import Counter
responses = [
"vanilla",
"chocolate",
"vanilla",
"vanilla",
"caramel",
"strawberry",
"vanilla"
]
print(
www.it-ebooks.info


Chapter 6

Download 2,95 Mb.

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