Author Dusty Phillips Reviewers



Download 2,95 Mb.
Pdf ko'rish
bet164/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   160   161   162   163   164   165   166   167   ...   183
Bog'liq
python3-oop

[
 169 
]
The first line inside the function uses a list comprehension to turn the 
CHARACTERS
list into a list of tuples. List comprehensions are an important, non-object-oriented 
tool in Python; we'll be covering them in detail in the next chapter.
Then we loop 
over each of the characters in the sentence. We first look up the index 
of the character in the 
CHARACTERS
list, which we know has the same index in our 
frequencies list, since we just created the second list from the first. We then update that 
index in the frequencies list by creating a new tuple, discarding the original one. Aside 
from the garbage collection and memory waste concerns, this is rather difficult to read!
Like dictionaries, lists are objects too, and they have several methods that can be 
invoked upon them. Here are some common ones:
• 
The 
append(element)
method adds an element to the end of the list
• 
The 
insert(index, element)
method inserts an item at a specific position
• 
The 
count(element)
method tells us how many times an element appears
in the list
• 
The 
index()
method tells us the index of an item in the list, raising an 
exception if it can't find it
• 
The 
find()
method does the same thing, but returns 
-1
instead of raising
an exception for missing items
• 
The 
reverse()
method does exactly what it says—turns the list around
• 
The 
sort()
method has some rather intricate object-oriented behaviors, 
which we'll cover now
Sorting lists
Without any parameters, 
sort
will generally do the expected thing. If it's a list of 
strings, it will place them in alphabetical order. This operation is case sensitive, so
all capital letters will be sorted before lowercase letters, that is 
Z
comes before 
a

If it is a list of numbers, they will be sorted in numerical order. If a list of tuples is 
provided, the list is sorted by the first element in each tuple. If a mixture containing 
unsortable items is supplied, the sort will raise a 
TypeError
exception.
www.it-ebooks.info


Python Data Structures

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   160   161   162   163   164   165   166   167   ...   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