Velocity Reviews > Convert from numbers to letters

# Convert from numbers to letters

rh0dium
Guest
Posts: n/a

 05-19-2005
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

Dan Sommers
Guest
Posts: n/a

 05-19-2005
On 19 May 2005 06:56:45 -0700,
"rh0dium" <(E-Mail Removed)> 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

(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..

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/>

Bill Mill
Guest
Posts: n/a

 05-19-2005
On 19 May 2005 06:56:45 -0700, rh0dium <(E-Mail Removed)> 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
>

Jason Drew
Guest
Posts: n/a

 05-19-2005
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

Steven Bethard
Guest
Posts: n/a

 05-19-2005
Bill Mill wrote:
>py> alpha = 'abcdefghijklmnopqrstuvwxyz'
>py> 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
> ...

It would probably be better to get in the habit of writing
globals()[x] = y
locals()[x] = y
You almost never want to do the latter[1]. The only reason it works in
this case is because, at the module level, locals() is globals().

You probably already knew this, but I note it here to help any newbies
avoid future confusion.

Steve

[1] For 99% of use cases. Modifying locals() might be useful if you're
just going to pass it to another function as a dict. But I think I've
seen *maybe* 1 use case for this.

qwweeeit@yahoo.it
Guest
Posts: n/a

 05-19-2005
Hi rh0dium,
Your request gives me the opportunity of showing a more realistic
example of the technique of "self-modification coding".
Although the coding is not as short as that suggested by the guys who
replayed to you, I think that it can be interesting....

# newVars.py
lCod=[]
for n in range(1,27):
.. lCod.append(chr(n+96)+'='+str(n)+'\n')
# other for-loops if you want define additional variables in sequence
(ex. aa,bb,cc etc...)
# write the variable definitions in the file "varDef.py"
fNewV=open('varDef.py','w')
fNewV.writelines(lCod)
fNewV.close()
from varDef import *
# ...
If you open the generated file (varDef.py) you can see all the variable
definitions, which are runned by "from varDef import *"
Bye.

rh0dium
Guest
Posts: n/a

 05-19-2005
Call me crazy.. But it doesn't work..

for i, digraph in enumerate(sorted([''.join((x, y)) for x in alpha for
y in [''] + [z for z in alpha]], key=len)):
globals()[digraph]=i+1

How do you implement this sucker??

Thanks

rh0dium
Guest
Posts: n/a

 05-19-2005
This is great but backwards...

Ok because you all want to know why.. I need to convert Excel columns
A2 into , [1,0] and I need a simple way to do that..

( The way this works is A->0 and 2->1 -- Yes they interchange -- So
B14 == [13,1] )

So my logic was simple convert the A to a number and then do the swap.
I didn't really care about the function so to speak it was a minor step
in the bigger picture..

By the way if you haven't played with pyXLWriter is it really good

So can anyone simply provide a nice function to do this? My logic was
along the same lines as Dans was earlier - but that just seems too
messy (and ugly)

Thanks

Bill Mill
Guest
Posts: n/a

 05-19-2005
On 19 May 2005 11:52:30 -0700, rh0dium <(E-Mail Removed)> wrote:
> Call me crazy.. But it doesn't work..
>

What doesn't work? What did python output when you tried to do it? It
is python 2.4 specific, it requires some changes for 2.3, and more for
earlier versions of python.

> for i, digraph in enumerate(sorted([''.join((x, y)) for x in alpha for
> y in [''] + [z for z in alpha]], key=len)):
> globals()[digraph]=i+1
>
> How do you implement this sucker??

Works just fine for me. Let me know what error you're getting and I'll

Peace
Bill Mill
bill.mill at gmail.com

Bill Mill
Guest
Posts: n/a

 05-19-2005
On 19 May 2005 11:59:00 -0700, rh0dium <(E-Mail Removed)> wrote:
> This is great but backwards...
>
> Ok because you all want to know why.. I need to convert Excel columns
> A2 into , [1,0] and I need a simple way to do that..
>
> ( The way this works is A->0 and 2->1 -- Yes they interchange -- So
> B14 == [13,1] )

why didn't you say this in the first place?

def coord2tuple(coord):
row, col = '', ''
alpha = 'abcdefghijklmnopqrstuvwxyz'.upper()
pairs = [''.join((x,y)) for x in alpha for y in [''] + [z for z in alpha]]
pairs = sorted(pairs, key=len)
coord = coord.upper()
for c in coord:
if c in alpha:
row += c
else:
col += c
return (int(col)-1, pairs.index(row))

>>> coord2tuple('B14')

(13, 1)
>>> coord2tuple('ZZ14')

(13, 701)
>>> coord2tuple('ZZ175')

(174, 701)
>>> coord2tuple('A2')

(1, 0)

Are there cols greater than ZZ? I seem to remember that there are not,
but I could be wrong.

Hope this helps.

Peace
Bill Mill
http://www.velocityreviews.com/forums/(E-Mail Removed)