Author Dusty Phillips Reviewers


>>> l = ["hello", "HELP", "Helo"]



Download 2,95 Mb.
Pdf ko'rish
bet167/183
Sana20.07.2022
Hajmi2,95 Mb.
#831085
1   ...   163   164   165   166   167   168   169   170   ...   183
Bog'liq
python3-oop

>>> l = ["hello", "HELP", "Helo"]
>>> l.sort()
>>> l
['HELP', 'Helo', 'hello']
>>> l.sort(key=str.lower)
>>> l
['hello', 'Helo', 'HELP']
Remember, even though 
lower
is a method on string objects, it is also a function that 
can accept a single argument, 
self
. In other words, 
str.lower(item)
is equivalent 
to 
item.lower()
. When we pass this function as a key, it performs the comparison 
on lowercase values instead of doing the default case-sensitive comparison.
There are a few sort key operations that are so common that the Python team has 
supplied them so you don't have to write them yourself. For example, it is often 
common to sort a list of tuples by something other than the first item in the list. 
The 
operator.itemgetter
method can be used as a key to do this:
>>> from operator import itemgetter
>>> l = [('h', 4), ('n', 6), ('o', 5), ('p', 1), ('t', 3), ('y', 2)]
>>> l.sort(key=itemgetter(1))
>>> l
[('p', 1), ('y', 2), ('t', 3), ('h', 4), ('o', 5), ('n', 6)]
The 
itemgetter
function is the most commonly used one (it works if the objects are 
dictionaries, too), but you will sometimes find use for 
attrgetter
and 
methodcaller

which return attributes on an object and the results of method calls on objects for the 
same purpose. See the 
operator
module documentation for more information.
www.it-ebooks.info


Chapter 6
[
 173 
]
Sets
Lists are extremely versatile tools that suit most container object applications. 
But they are not useful when we want to ensure objects in the list are unique. For 
example, a song library may contain many songs by the same artist. If we want to 
sort through the library and create a list of all the artists, we would have to check
the list to see if we've added the artist already, before we add them again.
This is where sets come in. Sets come from mathematics, where they represent an 
unordered group of (usually) unique numbers. We can add a number to a set five 
times, but it will show up in the set only once.
In Python, sets can hold any hashable object, not just numbers. Hashable objects 
are the same objects that can be used as keys in dictionaries; so again, lists and 
dictionaries are out. Like mathematical sets, they can store only one copy of each 
object. So if we're trying to create a list of song artists, we can create a set of string 
names and simply add them to the set. This example starts with a list of (song, artist) 
tuples and creates a set of the artists:
song_library = [("Phantom Of The Opera", "Sarah Brightman"),
("Knocking On Heaven's Door", "Guns N' Roses"),
("Captain Nemo", "Sarah Brightman"),
("Patterns In The Ivy", "Opeth"),
("November Rain", "Guns N' Roses"),
("Beautiful", "Sarah Brightman"),
("Mal's Song", "Vixy and Tony")]

Download 2,95 Mb.

Do'stlaringiz bilan baham:
1   ...   163   164   165   166   167   168   169   170   ...   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