Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet39/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   35   36   37   38   39   40   41   42   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Set manipulation

In this section we will illustrate some basic manipulations on sets using the following set,

containing three items:

s = {123, 54, 92}

The items in a set are not in any order and so it does not mean anything to access them

by index. In fact, there is no way to inquire about a specific item in a set. However, you

could  put  the  same  items  in  a  tuple  or  a  list  and  access  that,  but  even  then,  because  the

items in a set are unordered, the order in the resulting tuple or list is arbitrary:

x = tuple(s) # x now (123, 92, 54), s still a set

x = list(s) # x now [123, 92, 54], s still a set

You can check whether an item is present in a set, which is a computationally efficient

operation, like with dictionaries, but unlike lists and tuples:

54 in s # True

55 in s # False

You can also check whether an item is not in (absent from) a set:

54 not in s # False

55 not in s # True

The len() function returns the number of items in the set:

len(s) # 3

You can add items to a set:




s.add(77) # s now {123, 54, 92, 77}

If you try and add an item to a set that is already contained in the set then the operation

will effectively be ignored:

s.add(123) # s still {123, 54, 92, 77}

You can remove an item from a set:

s.remove(54) # s now {123, 54, 92, 77}

If you try to remove an item that is not in the set then you get an error.

You  can  fetch  and  remove  an  arbitrary  item  directly  from  a  set,  noting  that  here  the

pop() does not take any arguments:

y = s.pop() # s is one smaller, y is arbitrary

There are several functions that work with multiple sets, to do many of the operations

associated  with  set  theory  (as  are  often  illustrated  with  Venn  diagrams),  for  example,  to

find unions and intersections.

s = set{1, 2, 3, 4, 5}

t = set{4, 5, 6, 7}

a = s & t # Intersection: {4, 5}

b = s | t # Union: {1, 2, 3, 4, 5, 6, 7}

More set operations are listed in the reference section of the Appendices.




Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   35   36   37   38   39   40   41   42   ...   514




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