[Tutor] referencing a dictionary

Remco Gerlich scarblac@pino.selwerd.nl
Mon, 17 Jul 2000 22:13:44 +0200


On Mon, Jul 17, 2000 at 07:50:13PM +0000, brian callahan wrote:
> hows it going.  i had a quick (hopefully) question about using a dictionary. 
>   my idea is to use a raw_input string, to search a dictionary that contains 
> all the characters in the raw_input string with a value, and then i want to 
> sum the values.  my script is used to take a the amino acid sequence of a 
> protein and then give back the molecular weight.  i've been using the 
> 'learning python' book but im getting impatient.

So you have a dict of values, like {'a':3.1,'b':17.0,'c':1.0} etc and want
to sum a string using that dictionary?

dict = {....}
sum = 0
s = raw_input('enter the string:')
for char in s:
  if not dict.has_key(char):
    continue
  sum = sum+dict[char]

Should do it.

-- 
Remco Gerlich,  scarblac@pino.selwerd.nl