从数字转换为字母 [英] Convert from numbers to letters

查看:94
本文介绍了从数字转换为字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


虽然我知道有很多方法可以做到这一点..什么是最有效的?b $ b效率(就行数而言)只需这样做。


a = 1,b = 2,c = 3 ... z = 26


现在如果我们真的想要一些奖励积分..


a = 1,b = 2,c = 3 ... z = 26 aa = 27 ab = 28等...


谢谢

Hi All,

While I know there is a zillion ways to do this.. What is the most
efficient ( in terms of lines of code ) do simply do this.

a=1, b=2, c=3 ... z=26

Now if we really want some bonus points..

a=1, b=2, c=3 ... z=26 aa=27 ab=28 etc..

Thanks

推荐答案

2005年5月19日06:56:45 -0700,

rh0dium < SK **** @ pointcircle.com>写道:
On 19 May 2005 06:56:45 -0700,
"rh0dium" <sk****@pointcircle.com> wrote:
大家好,
虽然我知道有很多方法可以做到这一点..什么是最有效的(在代码行方面) )干脆做到这一点。
a = 1,b = 2,c = 3 ... z = 26


(a,b,c,d,e,f,g,h,i ,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z)=范围(1,27)

现在,如果我们真的想要一些奖励积分..
a = 1,b = 2,c = 3 ... z = 26 aa = 27 ab = 28等..
Hi All,
While I know there is a zillion ways to do this.. What is the most
efficient ( in terms of lines of code ) do simply do this. a=1, b=2, c=3 ... z=26
(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y ,z) = range( 1, 27 )
Now if we really want some bonus points.. a=1, b=2, c=3 ... z=26 aa=27 ab=28 etc..



它仍然是一行,按照上面的模式,只是更长。


现在为什么要这样做?

问候,

Dan


-

Dan Sommers

< http://www.tombstonezero.net/dan/>



It''s still one line, following the pattern from above, just longer.

Now why do you want to do this?

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>


2005年5月19日06:56:45 -0700,rh0dium< sk **** @ pointcircle.com>写道:
On 19 May 2005 06:56:45 -0700, rh0dium <sk****@pointcircle.com> wrote:
大家好,

虽然我知道有很多方法可以做到这一点..什么是最有效的(在代码行方面) )干脆做到这一点。

a = 1,b = 2,c = 3 ... z = 26
现在,如果我们真的想要一些奖励积分..

a = 1,b = 2,c = 3 ... z = 26 aa = 27 ab = 28等..


只是为了好玩,这里是使用listcomp执行此操作的一种方法。混淆了

蟒蛇粉丝,欢喜!
Hi All,

While I know there is a zillion ways to do this.. What is the most
efficient ( in terms of lines of code ) do simply do this.

a=1, b=2, c=3 ... z=26

Now if we really want some bonus points..

a=1, b=2, c=3 ... z=26 aa=27 ab=28 etc..

just for fun, here is one way to do it with a listcomp. Obfuscated
python fans, rejoice!
alpha =''abcdefghijklmnopqrstuvwxyz''
for i,digraph in enumerate(sorted([''''。join((x,y))for x in alpha in y in ['''] + [z for z in alpha]],key = len)):

.... locals()[digraph] = i + i

.... a
1 b
2 ac
29 dg
111 zz
702 26 ** 2 + 26
alpha = ''abcdefghijklmnopqrstuvwxyz''
for i, digraph in enumerate(sorted([''''.join((x, y)) for x in alpha for y in [''''] + [z for z in alpha]], key=len)):
.... locals()[digraph] = i + i
.... a 1 b 2 ac 29 dg 111 zz 702 26**2 + 26



702

谢谢

http://mail.python.org/mailman/listinfo/python-list


702
Thanks

--
http://mail.python.org/mailman/listinfo/python-list



想要设置似乎很奇怪实际变量中的值:a,b,

c,...,aa,ab,...,aaa,...,...


你在哪里划线?


功能似乎更合理。 在代码行方面这是

我的简洁方法:


nrFromDg = lambda dg:sum((ord(dg [x]) - ord('' a'')+ 1)*(26 **

(len(dg)-x-1))x in xrange(0,len(dg))))


然后,例如

nrFromDg(" bc")

给予

55



nrFromDg(" aaa")

给予

$

依此类推想要评估。


这在代码行方面是有效的,但当然函数

正在评估ord(a)和len (dg)多次,因此在避免冗余计算方面,它不是最有效的b $ b b。并且

nrFromDg(A)给你-31,所以你应该在评估之前将dg强制转换为

小写。哦,这很难读懂

lambda表达式。


最少量的代码 ==最佳解决方案

错误

It seems strange to want to set the values in actual variables: a, b,
c, ..., aa, ab, ..., aaa, ..., ...

Where do you draw the line?

A function seems more reasonable. "In terms of lines of code" here is
my terse way of doing it:

nrFromDg = lambda dg: sum(((ord(dg[x])-ord(''a'')+1) * (26 **
(len(dg)-x-1)) for x in xrange(0, len(dg))))

Then, for example
nrFromDg("bc")
gives
55
and
nrFromDg("aaa")
gives
703
and so on for whatever you want to evaluate.

This is efficient in terms of lines of code, but of course the function
is evaluating ord("a") and len(dg) multiple times, so it''s not the most
efficient in terms of avoiding redundant calculations. And
nrFromDg("A") gives you -31, so you should really force dg into
lowercase before evaluating it. Oh, and it''s pretty hard to read that
lambda expression.

"Least amount of code" == "best solution"
False


这篇关于从数字转换为字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆