使用字母值计算字符串的总和 [英] Calculating sum of a string using lettervalues
本文介绍了使用字母值计算字符串的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我花了太多时间试图解决看似非常简单的任务.
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?
推荐答案
lettermap = {
'o': 1,
'b': 2,
...
}
print sum(lettermap[c] for c in 'coffee')
这篇关于使用字母值计算字符串的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文