Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have spent way too much time trying to solve what seems to be a pretty easy task.

I have assigned some letters/variables with different values. Ex:

o,b,c,d,e,f = 1,2,3,4,5,6

and I want to find the "sum" of a string containing any of the letters, like this:

'coffee' = 3+1+6+6+5+5

Is there an easy/fast way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
144 views
Welcome To Ask or Share your Answers For Others

1 Answer

lettermap = {
  'o': 1,
  'b': 2,
   ...
}

print sum(lettermap[c] for c in 'coffee')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...