473,795 Members | 2,667 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 #1
30 21873
On 19 May 2005 06:56:45 -0700,
"rh0dium" <sk****@pointci rcle.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.tombstoneze ro.net/dan/>
Jul 19 '05 #2
On 19 May 2005 06:56:45 -0700, rh0dium <sk****@pointci rcle.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 = 'abcdefghijklmn opqrstuvwxyz'
for i, digraph in enumerate(sorte d([''.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

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

Jul 19 '05 #4
Bill Mill wrote:
py> alpha = 'abcdefghijklmn opqrstuvwxyz'
py> for i, digraph in enumerate(sorte d([''.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
instead of
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.
Jul 19 '05 #5
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('var Def.py','w')
fNewV.writeline s(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.

Jul 19 '05 #6
Call me crazy.. But it doesn't work..

for i, digraph in enumerate(sorte d([''.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

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

Jul 19 '05 #8
On 19 May 2005 11:52:30 -0700, rh0dium <sk****@pointci rcle.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(sorte d([''.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
help you figure it out.

Peace
Bill Mill
bill.mill at gmail.com
Jul 19 '05 #9
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 ))
coord2tuple('B1 4') (13, 1) coord2tuple('ZZ 14') (13, 701) coord2tuple('ZZ 175') (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
bi*******@gmail .com
Jul 19 '05 #10

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

Similar topics

7
4033
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
9131
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
2866
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
3151
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
3298
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
9262
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
9672
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
9519
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
10436
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
10213
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
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.