• No se han encontrado resultados

$he synta for !reating a %i!tionary is simiar to that for !reating a ist, b&t e &se !&ry bra!-ets rather than s<&are ones. )a!h pair of %ata, !onsisting of a -ey an% a  va&e, is !ae% an item. hen storing items in a %i!tionary, e separate them ith

!ommas. ithin an in%ivi%&a item, e separate the -ey an% the va&e ith a !oon. 7eres a bit of !o%e that !reates a %i!tionary of restri!tion enymes &sing %ata

from the previo&s !hapter= ith three items:

1HB ChapterI:Di!tionaries

'n this !ase, the -eys an% va&es are both strings1. "pitting the %i!tionary %efinition over severa ines ma-es it easier to rea%:

enEymes B U

9coQL ! r%AA))&, AaLL ! r%%(AT)#&&, NisL ! r%&$A)%&%& V

b&t %oesnt affe!t the !o%e at a. $o retrieve a bit of %ata from the %i!tionary 5 i.e. to oo- &p the motif for a parti!&ar enyme 5 e rite the name of the %i!tionary, fooe% by the -ey in s<&are bra!-ets:

print(enEymes$NisL#

$he !o%e oo-s very simiar to &sing a ist, b&t instea% of giving the in%e of the eement e ant, ere giving the key  for the value that e ant to retrieve. Di!tionaries are a very &sef& ay to store %ata, b&t they !ome ith some

restri!tions. $he ony types of %ata e are aoe% to &se as -eys are strings an% n&mbers2, so e !ant, for eampe, !reate a %i!tionary here the -eys are fie ob9e!ts. Sa&es !an be hatever type of %ata e i-e. 4so, -eys must be &ni<&e 5  e !ant store m&tipe va&es for the same -ey.

'n reaife programs, its reativey rare that e ant to !reate a %i!tionary a in one go i-e in the eampe above. More often, e ant to !reate an empty

%i!tionary, then a%% -ey/va&e pairs to it 9&st as e often !reate an empty ist an% then a%% eements to it=.

$o !reate an empty %i!tionary e simpy rite a pair of !&ry bra!-ets on their on, an% to a%% eements, e &se the s<&arebra!-ets notation on the efthan% si%e of

1 $he va&es are a!t&ay ra strings, b&t thats not important.

1H ChapterI:Di!tionaries

an assignment. 7eres a bit of !o%e that stores the restri!tion enyme %ata one item at a time:

enEymes B UV

enEymes$9coQL B r%AA))& enEymes$AaLL B r%%(AT)#&& enEymes$NisL B r%&$A)%&%&

e !an %eete a -ey from a %i!tionary &sing the pop metho%. pop a!t&ay ret&rns the va&e an% %eetes the -ey at the same time:

enEymes B U

9coQL ! r%AA))&, AaLL ! r%%(AT)#&&, NisL ! r%&$A)%&%& V

4 remoe the 9coQL enEyme from the dict enEymes.pop(9coQL#

+ets ta-e another oo- at the trin&!eoti%e !o&nt eampe from the start of the !hapter. 7eres ho e store the trin&!eoti%es an% their !o&nts in a %i!tionary:

dna B *AA)%A)&%A)&%)A&%&)%A* counts B UV

for -ase1 in $A, ), %, &! for -ase2 in $A, ), %, &!

for -ase3 in $A, ), %, &!

trinucleotide B -ase1 C -ase2 C -ase3 count B dna.count(trinucleotide#

counts$trinucleotide B count print(counts#

e !an see from the o&tp&t that the trin&!eoti%es an% their !o&nts are store% together in one variabe:

1HG ChapterI:Di!tionaries

UA&&! 0, A)%! 1, AA%! 0, AAA! 0, A)&! 2, AA&! 0, A)A! 0, A%%! 0, &&)! 0, &)&! 0, A%&! 0, A&A! 0, A%A! 0, &A)! 0, AA)! 1, A))! 0, &)%! 1, &)A! 0, A&)! 0, &A&! 0, A&%! 1, &AA! 0, A%)! 0, &A%! 0, &&%! 0, &&&! 0, &))! 0, )A)! 0, %%)! 0, )%)! 0, &%A! 1, &&A! 0, )&)! 0, %A)! 2, &%%! 0, )))! 0, )%&! 0, %%%! 0, )A%! 0, %%A! 0, )AA! 0, %%&! 0, )A&! 1, ))&! 0, )&%! 2, ))A! 0, ))%! 0, )&&! 0, %AA! 0, )%%! 0, %&A! 0, %)A! 1, %&&! 0, %)&! 0, %&%! 0, %)%! 0, %A%! 0, %))! 0, %&)! 1, )%A! 2, %A&! 0, &%)! 1, )&A! 0, &%&! 1V

e sti have a ot of repetitive !o&nts of ero, b&t oo-ing &p the !o&nt for a parti!&ar trin&!eoti%e is no very straightforar%:

print(counts$)%A#

e no onger have to orry abo&t either >memoriing> the or%er of the !o&nts or maintaining to separate ists.

+ets no see if e !an fin% a ay of avoi%ing storing a those ero !o&nts. e !an a%% an if statement that ens&res that e ony store a !o&nt if its greater than ero:

dna B *AA)%A)&%A)&%)A&%&)%A* counts B UV

for -ase1 in $A, ), %, &! for -ase2 in $A, ), %, &!

for -ase3 in $A, ), %, &!

trinucleotide B -ase1 C -ase2 C -ase3 count B dna.count(trinucleotide#

if count + 0!

counts$trinucleotide B count print(counts#

1HH ChapterI:Di!tionaries

hen e oo- at the o&tp&t from the above !o%e, e !an see that the amo&nt of %ata ere storing is m&!h smaer 5 9&st the !o&nts for the trin&!eoti%es that are greater than ero:

UA)%! 1, A&%! 1, A)&! 2, %)A! 1, &)%! 1, &%&! 1, %A)! 2, &%A! 1, AA)! 1, )%A! 2, %&)! 1, )A&! 1, )&%! 2, &%)! 1V

;o e have a ne probem to %ea ith. +oo-ing &p the !o&nt for a given trin&!eoti%e or-s fine hen the !o&nt is positive:

print(counts$)%A#

(&t hen the !o&nt is ero, the trin&!eoti%e %oesnt appear as a -ey in the %i!tionary:

print(counts$AAA#

so e i get a Key)rror hen e try to oo- it &p:

Iey9rror! AAA

$here are to possibe ays to fi this. e !an !he!- for the eisten!e of a -ey in a %i!tionary 9&st i-e e !an !he!- for the eisten!e of an eement in a ist=, an% ony  try to retrieve it on!e e -no it eists:

if AAA in counts!

1HI ChapterI:Di!tionaries

4ternativey, e !an &se the %i!tionarys get metho%. get &s&ay or-s 9&st i-e &sing s<&are bra!-ets: the fooing to ines %o ea!ty the same thing:

print(counts$)%A#

print(counts.get()%A##

$he thing that ma-es get reay &sef&, hoever, is that it !an ta-e an optiona se!on% arg&ment, hi!h is the %efa&t va&e to be ret&rne% if the -ey isnt present in the %i!tionary. 'n this !ase, e -no that if a given trin&!eoti%e %oesnt appear in the %i!tionary then its !o&nt is ero, so e !an give ero as the %efa&t va&e an% &se get to print o&t the !o&nt for any trin&!eoti%e:

print(*count for )%A is * C str(counts.get()%A, 0### print(*count for AAA is * C str(counts.get(AAA, 0### print(*count for %)A is * C str(counts.get(%)A, 0### print(*count for ))) is * C str(counts.get())), 0###

4s e !an see from the o&tp&t, e no %ont have to orry abo&t hether or not ea!h trin&!eoti%e appears in the %i!tionary 5 get ta-es !are of everything an% ret&rns ero hen appropriate:

count for )%A is 2 count for AAA is 0 count for %)A is 1 count for ))) is 0

1H@ ChapterI:Di!tionaries

 terating over a "ictionary 

hat if, instea% of oo-ing &p a singe item from a %i!tionary, e ant to %o

something for a itemsE For eampe, imagine that e ante% to ta-e o&r !o&nts %i!tionary variabe from the !o%e above an% print o&t a trin&!eoti%es here the !o&nt as 2. ?ne ay to %o it o&% be to &se o&r three neste% oops again to

generate a possibe trin&!eoti%es, then oo- &p the !o&nt for ea!h one an% %e!i%e  hether or not to print it:

for -ase1 in $A, ), %, &! for -ase2 in $A, ), %, &!

for -ase3 in $A, ), %, &!

trinucleotide B -ase1 C -ase2 C -ase3 if counts.get(trinucleotide, 0# BB 2!

print(trinucleotide#

4s e !an see from the o&tp&t, this or-s perfe!ty e:

A)& )%A )&% %A)

(&t it seems ineffi!ient to go thro&gh the hoe pro!ess of generating a possibe trin&!eoti%es again, hen the information e ant 5 the ist of trin&!eoti%es 5 is area%y in the %i!tionary. 4 better approa!h o&% be to rea% the ist of -eys

%ire!ty from the %i!tionary, hi!h is hat the keys metho% %oes.