Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Convert from numbers to letters

Reply
Thread Tools

Convert from numbers to letters

 
 
rh0dium
Guest
Posts: n/a
 
      05-19-2005
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 "<stdin>", line 1, in ?
NameError: name 'sorted' is not defined

I think you're probably using 2.4 ??

 
Reply With Quote
 
 
 
 
Bill Mill
Guest
Posts: n/a
 
      05-19-2005
On 19 May 2005 12:20:03 -0700, rh0dium <(E-Mail Removed)> 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 "<stdin>", 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:

def sorted(lst, **kwargs):
l2 = lst[:]
if kwargs.has_key('key'):
f = kwargs['key']
l2.sort(lambda a,b: cmp(f(a), f(b)))
return l2
l2.sort()
return l2

And from your other email:
> I need to go the other way! tuple2coord


Sorry, I only go one way. It should be transparent how to do it backwards.

Peace
Bill Mill
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      05-19-2005
Bill Mill wrote:

>> Traceback (most recent call last):
>>File*"<stdin>",*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() can be removed from your original post.

Code has no effect

Peter


 
Reply With Quote
 
Bill Mill
Guest
Posts: n/a
 
      05-19-2005
On 5/19/05, Peter Otten <(E-Mail Removed)> wrote:
> Bill Mill wrote:
>
> >> Traceback (most recent call last):
> >>File"<stdin>",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
> > tested pure-python partial replacement:

>
> 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 alpha for y in [''] + [z for z in alpha]]
False

If you want to see why, here's a small example:

>>> alpha = 'abc'
>>> [''.join((x,y)) for x in alpha for y in [''] + [z for z in alpha]]

['a', 'aa', 'ab', 'ac', 'b', 'ba', 'bb', 'bc', 'c', 'ca', 'cb', 'cc']

>>> sorted([''.join((x,y)) for x in alpha for y in [''] + [z for z in alpha]],

key=len)
['a', 'b', 'c', 'aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']

Peace
Bill Mill
bill.mill at gmail.com
 
Reply With Quote
 
Gary Wilson Jr
Guest
Posts: n/a
 
      05-19-2005
Bill Mill wrote:
> On 5/19/05, Peter Otten <(E-Mail Removed)> wrote:
>
>>Bill Mill wrote:
>>
>>
>>>>Traceback (most recent call last):
>>>>File"<stdin>",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
>>>tested pure-python partial replacement:

>>
>>By the way, sorted() can be removed from your original post.
>>
>>Code has no effect

>
>
> I'm gonna go ahead and disagree with you:


Me too, although I would forgo the sort altogether (while making things
a little more readable IMO):

> alpha = 'abcdefghijklmnopqrstuvwxyz'.upper()
> pairs = [''.join((x,y)) for x in alpha for y in [''] + [z for z in alpha]]
> pairs = sorted(pairs, key=len)


alpha = 'abcdefghijklmnopqrstuvwxyz'.upper()
pairs = [x for x in alpha] + [''.join((x,y)) for x in alpha for y in alpha]



 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      05-19-2005
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 alpha for y in [''] + [z for z in alpha]]
> False
>


That's not your original code. You used the contents to modify the locals()
(effectively globals()) dictionary:

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


Of course you lose the order in that process.
When you do care about order, I suggest that you swap the for clauses
instead of sorting, e. g:

>>> alpha = list("abc")
>>> items = [x + y for x in [""] + alpha for y in alpha]
>>> items == sorted(items, key=len)

True

Peter

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      05-19-2005
Peter Otten wrote:

[Something stupid]

You are right. I finally got it.

Peter



 
Reply With Quote
 
Jason Drew
Guest
Posts: n/a
 
      05-19-2005
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].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)) if \
cp[i].isdigit()])])-x-1)) for x in \
xrange(0, len(cp[0:min([i for i in \
xrange(0, len(cp)) if \
cp[i].isdigit()])]))))-1)

print z("B14")
# gives (13, 1)

Maybe brevity isn't the soul of wit after all ...

 
Reply With Quote
 
Gary Wilson Jr
Guest
Posts: n/a
 
      05-19-2005
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)?
 
Reply With Quote
 
Mike Meyer
Guest
Posts: n/a
 
      05-19-2005
Bill Mill <(E-Mail Removed)> writes:

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


That seems like the long way around. Python can search strings for
substrings, so why not use that? That gets the search loop into C
code, where it should be faster.

from string import uppercase

def coord2tuple2(coord):
if len(coord) > 1 or uppercase.find(coord) < 0:
raise ValueError('coord2tuple2 expected a single uppercase character, got "%s"' % coord)
return uppercase.index(coord) + 1

Without the initial test, it has a buglet of return values for "AB"
and similar strings. If searching uppercase twice really bothers you,
you can drop the uppercase.find; then you'll get less informative
error messages if coord2tuple2 is passed single characters that aren't
in uppercase.

<mike
--
Mike Meyer <(E-Mail Removed)> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
convert Phone Letters to Phone numbers Rene Ivon Shamberger C++ 5 12-05-2012 04:11 PM
making all letters Caps/Small Letters Merrigan Python 4 12-14-2007 10:10 AM
question about making graphics with letters and numbers Robin Computer Support 4 05-19-2007 06:49 AM
Changing from capital letters to small letters using perl Venugopal Perl Misc 11 11-05-2003 06:07 PM
Numbers or letters in a TextBox Pablo ASP .Net 2 08-26-2003 04:44 PM



Advertisments