Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Convert from numbers to letters (http://www.velocityreviews.com/forums/t345138-convert-from-numbers-to-letters.html)

 rh0dium 05-19-2005 01:56 PM

Convert from numbers to letters

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 05-19-2005 02:24 PM

Re: Convert from numbers to letters

On 19 May 2005 06:56:45 -0700,
"rh0dium" <sklass@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

(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 05-19-2005 02:42 PM

Re: Convert from numbers to letters

On 19 May 2005 06:56:45 -0700, rh0dium <sklass@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
>

 Jason Drew 05-19-2005 04:13 PM

Re: Convert from numbers to letters

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 05-19-2005 04:40 PM

Re: Convert from numbers to letters

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 05-19-2005 05:58 PM

Re: Convert from numbers to letters

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 05-19-2005 06:52 PM

Re: Convert from numbers to letters

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 05-19-2005 06:59 PM

Re: Convert from numbers to letters

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 05-19-2005 07:00 PM

Re: Convert from numbers to letters

On 19 May 2005 11:52:30 -0700, rh0dium <sklass@pointcircle.com> 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 05-19-2005 07:19 PM

Re: Convert from numbers to letters

On 19 May 2005 11:59:00 -0700, rh0dium <sklass@pointcircle.com> 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
bill.mill@gmail.com

All times are GMT. The time now is 09:28 PM.