Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet21/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   17   18   19   20   21   22   23   24   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Dictionaries

A  dictionary  is  a  Python  data  structure  which  associates  pairs  of  data  objects  to  create  a

look-up  table.  The  first  object  in  the  pair  is  called  the  key,  and  is  unique  inside  a  given

dictionary, and the second object is its value. Unlike lists, where you refer to items by their

position in a sequence, with dictionaries the data is not stored in any particular order and

you access the values contained within by using the key. For example, you could have a

dictionary  which  records  the  heights  of  various  mountains,  where  you  find  the  correct

height for the correct mountain by using the name of the mountain (a string, the key) to

look up the height (the value). In this instance if you were using a list you would have to

know in which order the  heights  were  stored,  but  with  a  dictionary  you  do  not.  You  can

have  empty  dictionaries  and  add  and  remove  data  from  dictionaries,  by  adding  and

removing the pairs of key and value. The value that a particular key finds can be altered at

any  time,  and  although  a  key  can  only  refer  to  one  value,  the  value  could  be  a  data

structure  object,  like  a  list  or  set,  that  contains  other  items.  In  Python  we  use  curly

brackets to specify the beginning and end of a dictionary.

ageDict = {'homer':36, 'marge':34, 'lisa':8, 'bart':10}

print(ageDict['lisa'])

Defining a dictionary in Python, which in this example allows an age value to be accessed using a

name as the key.

An important point to be aware of is that while any type of data object can be a value in

a  dictionary,  only  certain  kinds  of  object  (those  that  cannot  be  internally  modified  to

assume a different identity

5

) can be used as keys. Put verbosely: text strings,



6

 tuples,  the

True and False objects, the None object, user-defined objects and frozen sets can be used

as keys to access dictionary data, but lists, normal sets and other dictionaries cannot. The

reason  behind  this  rule  is  that  the  values  in  a  dictionary  are  efficiently  accessed  based

upon knowledge of the route from their keys, so Python must be able to consistently and

uniquely identify each key and thus get the correct location of each value.


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   17   18   19   20   21   22   23   24   ...   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