Author Dusty Phillips Reviewers


Vixy and Tony play good music



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

Vixy and Tony play good music
Opeth plays good music
>>> alphabetical = list(artists)
>>> alphabetical.sort()
>>> alphabetical
["Guns N' Roses", 'Opeth', 'Sarah Brightman', 'Vixy and Tony']
While the primary 
feature
of a set is uniqueness, that is not its primary 
purpose
. Sets 
are most useful when two or more of them are used in combination. Most of the 
methods on the set type operate on other sets, allowing us to efficiently combine or 
compare the items in two or more sets. These methods have strange names, since 
they use the same terminology used in mathematics. We'll start with three methods 
that return the same result, regardless of which is the calling set and which is the 
called set.
The 
union
method is the most common and easiest to understand. It takes a second 
set as a parameter and returns a new set that contains all elements that are in 
either
of the two sets; if an element is in both original sets, it will, of course, only show up 
once in the new set. Union is like a logical 
or
operation, indeed, the 
|
operator can
be used on two sets to perform the union operation, if you don't like calling methods.
www.it-ebooks.info


Chapter 6
[
 175 
]
Conversely, the intersection method accepts a second set and returns a new set that 
contains only those elements that are in 
both
sets. It is like a logical 
and
operation, 
and can also be referenced using the 
&
operator.
Finally, the 
symmetric_difference
method tells us what's left; it is the set of objects 
that are in one set or the other, but not both. The following example illustrates these 
methods by comparing some artists from my song library to those in my sister's:
my_artists = {"Sarah Brightman", "Guns N' Roses",
"Opeth", "Vixy and Tony"}
auburns_artists = {"Nickelback", "Guns N' Roses",
"Savage Garden"}
print("All: {}".format(my_artists.union(auburns_artists)))
print("Both: {}".format(auburns_artists.intersection(my_artists)))
print("Either but not both: {}".format(
my_artists.symmetric_difference(auburns_artists)))
If we run this code, we see that these three methods do what the print statements 
suggest they will do:

Download 2,95 Mb.

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