Convert from numbers to letters

2005-05-19 Thread rh0dium
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 -- http://mail.python.org/mailman/listinfo/p

Re: Convert from numbers to letters

2005-05-19 Thread Dan Sommers
On 19 May 2005 06:56:45 -0700, "rh0dium" <[EMAIL PROTECTED]> 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) = ra

Re: Convert from numbers to letters

2005-05-19 Thread Bill Mill
On 19 May 2005 06:56:45 -0700, rh0dium <[EMAIL PROTECTED]> 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,

Re: Convert from numbers to letters

2005-05-19 Thread Jason Drew
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

Re: Convert from numbers to letters

2005-05-19 Thread Steven Bethard
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 glob

Re: Convert from numbers to letters

2005-05-19 Thread qwweeeit
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(

Re: Convert from numbers to letters

2005-05-19 Thread rh0dium
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 -- http://mail.python.org/mailman/listinfo/python-list

Re: Convert from numbers to letters

2005-05-19 Thread rh0dium
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 the

Re: Convert from numbers to letters

2005-05-19 Thread Bill Mill
On 19 May 2005 11:52:30 -0700, rh0dium <[EMAIL PROTECTED]> 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 i

Re: Convert from numbers to letters

2005-05-19 Thread Bill Mill
On 19 May 2005 11:59:00 -0700, rh0dium <[EMAIL PROTECTED]> 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 --

Re: Convert from numbers to letters

2005-05-19 Thread rh0dium
Python 2.3.5 (#1, Mar 20 2005, 20:38:20) [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin Traceback (most recent call last): File "", line 1, in ? NameError: name 'sorted' is not defined I think you're probably using 2.4 ?? -- http://mail.python.org/mailman/listinfo/python-list

Re: Convert from numbers to letters

2005-05-19 Thread Bill Mill
On 19 May 2005 12:20:03 -0700, rh0dium <[EMAIL PROTECTED]> wrote: > Python 2.3.5 (#1, Mar 20 2005, 20:38:20) > [GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin > > Traceback (most recent call last): > File "", line 1, in ? > NameError: name 'sorted' is not defined > > I think you'

Re: Convert from numbers to letters

2005-05-19 Thread Peter Otten
Bill Mill wrote: >> Traceback (most recent call last): >>File "", line 1, in ? >> NameError: name 'sorted' is not defined >> >> I think you're probably using 2.4 ?? > > Yes, sorted() is new in python 2.4 .You could use a very lightly > tested pure-python partial replacement: By the way, sorted(

Re: Convert from numbers to letters

2005-05-19 Thread Bill Mill
On 5/19/05, Peter Otten <[EMAIL PROTECTED]> wrote: > Bill Mill wrote: > > >> Traceback (most recent call last): > >>File"",line1,in? > >> NameError: name 'sorted' is not defined > >> > >> I think you're probably using 2.4 ?? > > > > Yes, sorted() is new in python 2.4 .You could use a very lightly

Re: Convert from numbers to letters

2005-05-19 Thread Gary Wilson Jr
Bill Mill wrote: > On 5/19/05, Peter Otten <[EMAIL PROTECTED]> wrote: > >>Bill Mill wrote: >> >> Traceback (most recent call last): File"",line1,in? NameError: name 'sorted' is not defined I think you're probably using 2.4 ?? >>> >>>Yes, sorted() is new in python 2.4 .You coul

Re: Convert from numbers to letters

2005-05-19 Thread Peter Otten
Bill Mill wrote: >> By the way, sorted() can be removed from your original post. >> >> Code has no effect :-) > > I'm gonna go ahead and disagree with you: > sorted([''.join((x, y)) for x in alpha \ > ...for y in [''] + [z for z in alpha]], key=len) == \ > ... [''.join((x,y)) for x in

Re: Convert from numbers to letters

2005-05-19 Thread Peter Otten
Peter Otten wrote: [Something stupid] You are right. I finally got it. Peter -- http://mail.python.org/mailman/listinfo/python-list

Re: Convert from numbers to letters

2005-05-19 Thread Jason Drew
We weren't really backwards; just gave a full solution to a half-stated problem. Bill, you've forgotten the least-lines-of-code requirement :-) Mine's still a one-liner (chopped up so line breaks don't break it): z = lambda cp: (int(cp[min([i for \ i in xrange(0, len(cp)) if \ cp[i].isdi

Re: Convert from numbers to letters

2005-05-19 Thread Gary Wilson Jr
Gary Wilson Jr wrote: > alpha = 'abcdefghijklmnopqrstuvwxyz'.upper() > pairs = [x for x in alpha] + [''.join((x,y)) for x in alpha for y in alpha] I forget, is string concatenation with '+' just as fast as join() now (because that would look even nicer)? -- http://mail.python.org/mailman/listinfo

Re: Convert from numbers to letters

2005-05-19 Thread Mike Meyer
Bill Mill <[EMAIL PROTECTED]> writes: > On 19 May 2005 11:59:00 -0700, rh0dium <[EMAIL PROTECTED]> 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 wo

Re: Convert from numbers to letters

2005-05-19 Thread Steven Bethard
Jason Drew wrote: > z = lambda cp: (int(cp[min([i for \ > i in xrange(0, len(cp)) if \ > cp[i].isdigit()]):])-1, > sum(((ord(cp[0:min([i for i in \ > xrange(0, len(cp)) if \ > cp[i].isdigit()])][x])-ord('A')+1) \ > * (26 ** (len(cp[0:min([i for i in \ > xrange(0, len(cp)

Re: Convert from numbers to letters

2005-05-19 Thread Steven Bethard
Gary Wilson Jr wrote: > Gary Wilson Jr wrote: > >>alpha = 'abcdefghijklmnopqrstuvwxyz'.upper() >>pairs = [x for x in alpha] + [''.join((x,y)) for x in alpha for y in alpha] > > I forget, is string concatenation with '+' just as fast as join() > now (because that would look even nicer)? Certain l

Re: Convert from numbers to letters

2005-05-19 Thread Jason Drew
Oh yeah, oops, thanks. (I mean the line continuations, not the alleged sin against man and nature, an accusation which I can only assume is motivated by jealousy :-) Or fear? They threw sticks at Frankenstein's monster too. And he turned out alright. My elegant "line" of code started out without t

Re: Convert from numbers to letters

2005-05-19 Thread rh0dium
Wow - now that is ugly.. But it is effective. I would love a cleaner version - but I did say brevity :) Nice work. -- http://mail.python.org/mailman/listinfo/python-list

Re: Convert from numbers to letters

2005-05-19 Thread rh0dium
Now can you reverse this process tuple2coord?? Thats what I'm really after :) -- http://mail.python.org/mailman/listinfo/python-list

Re: Convert from numbers to letters

2005-05-19 Thread Mike Meyer
"rh0dium" <[EMAIL PROTECTED]> writes: > Now can you reverse this process tuple2coord?? You didn't provide enough context to know who you're asking, but here's the inverse of my coord2tuple2 function: from string import uppercase def tuple2coord(number): if 1 > number or number > 26: ra

Re: Convert from numbers to letters

2005-05-20 Thread Jason Drew
Er, yes! It's REALLY ugly! I was joking (though it works)! I retract it from the code universe. (But patent pending nr. 4040404.) Here's how I really would convert your (row_from_zero, col_from_zero) tuple to spreadsheet "A1" coords, in very simple and easy to read code. ##def tuple2coord(tupl):

Re: Convert from numbers to letters

2005-05-20 Thread Jason Drew
Sorry, scratch that "P.S."! The act of hitting Send seems to be a great way of realising one's mistakes. Of course you need colnr - m for those times when m is set to 26. Remembered that when I wrote it, forgot it 2 paragraphs later! -- http://mail.python.org/mailman/listinfo/python-list

Re: Convert from numbers to letters

2005-05-20 Thread Steven Bethard
Jason Drew wrote: > ##def tuple2coord(tupl): [snip] > ##rowfromzero, colfromzero = tupl Just a side note here that if you want a better function signature, you might consider writing this as: tuple2coord((rowfromzero, colfromzero)): ... Note that the docstrings are nicer this way: py>

Re: Convert from numbers to letters

2005-05-20 Thread Jason Drew
Hey, that's good. Thanks Steve. Hadn't seen it before. One to use. Funny that Pythonwin's argument-prompter (or whatever that feature is called) doesn't seem to like it. E.g. if I have def f(tupl): print tupl Then at the Pythonwin prompt when I type f( I correctly get "(tupl)" in the argumen

Re: Convert from numbers to letters

2005-05-20 Thread Bill Mill
On 20 May 2005 10:07:55 -0700, Jason Drew <[EMAIL PROTECTED]> wrote: > Hey, that's good. Thanks Steve. Hadn't seen it before. One to use. > > Funny that Pythonwin's argument-prompter (or whatever that feature is > called) doesn't seem to like it. > > E.g. if I have > def f(tupl): > print tupl