473,804 Members | 3,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 19 '05
30 21878
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 ??

Jul 19 '05 #11
On 19 May 2005 12:20:03 -0700, rh0dium <sk****@pointci rcle.com> 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
bi*******@gmail .com
Jul 19 '05 #12
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
Jul 19 '05 #13
On 5/19/05, Peter Otten <__*******@web. de> 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
Jul 19 '05 #14
Bill Mill wrote:
On 5/19/05, Peter Otten <__*******@web. de> 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 = 'abcdefghijklmn opqrstuvwxyz'.u pper()
pairs = [''.join((x,y)) for x in alpha for y in [''] + [z for z in alpha]]
pairs = sorted(pairs, key=len)


alpha = 'abcdefghijklmn opqrstuvwxyz'.u pper()
pairs = [x for x in alpha] + [''.join((x,y)) for x in alpha for y in alpha]

Jul 19 '05 #15
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*alph a]],*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 = 'abcdefghijklmn opqrstuvwxyz'
for i, digraph in enumerate(sorte d([''.join((x, y)) for x in alpha \ for*y*in*['']*+*[z*for*z*in*alph a]],*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

Jul 19 '05 #16
Peter Otten wrote:

[Something stupid]

You are right. I finally got it.

Peter

Jul 19 '05 #17
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 ...

Jul 19 '05 #18
Bill Mill <bi*******@gmai l.com> writes:
On 19 May 2005 11:59:00 -0700, rh0dium <sk****@pointci rcle.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(coo rd):
row, col = '', ''
alpha = 'abcdefghijklmn opqrstuvwxyz'.u pper()
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(co ord):
if len(coord) > 1 or uppercase.find( coord) < 0:
raise ValueError('coo rd2tuple2 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 <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #19
Gary Wilson Jr wrote:
alpha = 'abcdefghijklmn opqrstuvwxyz'.u pper()
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)?
Jul 19 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
4035
by: Gerard Flanagan | last post by:
All would anyone happen to have code to generate Cutter Numbers: eg. http://www1.kfupm.edu.sa/library/cod-web/Cutter-numbers.htm or is anyone looking for something to do?-) (I'm under pressure!) def cutter(lname, fname, book_title):
22
18215
by: federico_bertola | last post by:
Hi everybody, I have an array of chars that I want to make all lower int Scan(char Search) { char *cPtr; cPtr = strtok (Search," -,."); while (cPtr != NULL) {
2
331
by: karups | last post by:
Hi when i convert Excel file to dataset using the following code, i find that, some col. such as Col1 ------ 404 403 NOT 222
9
9132
by: Paul | last post by:
Hi, I have spent the last couple of days researching this issue. And I have also spent time thinking about what is needed. I am distributing my software as shareware. When a customer orders a license, I send him/her a printed license with a license number that can be used to unlock certain features in the software. A license number should consist of up to 20 numbers and letters. Upper/lower case letters should not matter and in order...
8
2867
by: flyingisfun1217 | last post by:
Hey, Sorry to bother everybody again, but this group seems to have quite a few knowledgeable people perusing it. Here's my most recent problem: For a small project I am doing, I need to change numbers into letters, for example, a person typing in the number '3', and getting the output 'Three'. So far, I have an interface that only collects numbers (or letters), and displays them in a text variable label (as you can see below). Heres...
2
5090
by: Tom | last post by:
I need to convert an integer to a GUID consisting only of capital letters and numbers. I also need to be able to convert it back again. I would prefer it was somewhat difficult to determine how to convert it back. What do I do? :-) Regards
1
3152
by: Jeff | last post by:
hey gang. I have a code to create a random string of letters. The number of them can be whatever I desire. what i would like to do, is have it both letters and integers. how would i modify this code to allow that. '***** make random password ****** Sub StrRandomize(strSeed)
5
3301
by: lim4801 | last post by:
I am currently in doing a program which is given by my tutor: Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are "special" because they are easily translated into words. You've been asked to create a list of phone numbers that are directly mappable to words by searching a dictionary for every 7 or 10 letter word that maps on to the phone lettering scheme: ...
3
9263
by: phub11 | last post by:
Hi all, I was wondering if there is a quick way to convert numbers to letters, such as 1=A, and 26=Z. I can do it the laborious way using 26 definitions, but it's always nice to know of any shortcuts! Thanks
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10578
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.