编码高尔夫!有没有一种简单的方法可以在Python中将字母转换为数字? [英] Code golf! Is there a simple way to convert letters to numbers in Python?
本文介绍了编码高尔夫!有没有一种简单的方法可以在Python中将字母转换为数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您知道,例如A = 1,B = 2等等.我可以列出很长的if-thens列表,但是也许已经有一个模块了.
You know, like A = 1, B = 2 etc. I could just make a long list of if-thens, but maybe there's already a module for it.
如果其工作方式与"Excel坐标"(AA = 27并继续)一样,则奖励.(这算作base26数字吗?)
Bonus if it works like it does in "Excel Coordinates" where AA = 27 and continues. (Does this count as base26 numbers?)
推荐答案
def foo(c):
return ord(c) - 64
foo('A')
1
foo('B')
2
从我的头顶上跳下来:p
off the top of my head :p
这篇关于编码高尔夫!有没有一种简单的方法可以在Python中将字母转换为数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文