Convert from numbers to letters

Bill Mill bill.mill at gmail.com
Thu May 19 10:42:58 EDT 2005


On 19 May 2005 06:56:45 -0700, rh0dium <sklass at pointcircle.com> wrote:
> 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 \
    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

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



More information about the Python-list mailing list