[Tutor] Iterating over letters or arbitrary symbols like they were numbers...
John Fouhy
john at fouhy.net
Thu Mar 19 02:11:25 CET 2009
2009/3/19 Alexander Daychilde (Gmail) <daychilde at gmail.com>:
> That creates a list of numbers. I also need to do letters. That is, treat
> a-z as base 26, and do the same thing. The three examples I gave from before
> would be:
> 1:9 --> a:z
> 1:99 --> a:zz
> 01:99 -- no "zero" in alpha to worry about, so no padding
> necessary...
>
> So my first question is: Can I somehow treat letters like base 26?
>
> If I can, what about alphanumeric, i.e. 0-9+a-z would be like base 36...
The int() function takes a second parameter, which is the base. This
is so that it can deal with hex strings -- for example, int('7a', 16)
== 122. But the base can actually be anything up to 36. For
converting a base n string to an integer, the digits are the first n
elements of '0123456789abcdefghijklmnopqrstuvwxyz'.
To go the other way, I had a quick poke around in the python cookbook
and found this one-liner:
def baseN(num,b,numerals="0123456789abcdefghijklmnopqrstuvwxyz"):
return ((num == 0) and "0" ) or ( baseN(num // b, b).lstrip("0")
+ numerals[num % b])
You may be able to find better ones.
If you want to convert 'a'..'z' to integers by treating them as
base-26 numbers.. it could be tricky because you (I think) have no 0.
Multiplication without a 0 is really quite tricky :-) You might be
better off writing a function to take one "integer" (e.g. 'ghzz') and
produce the next one ('giaa').
Or you could cheat :-)
>>> import string
>>> letters = string.lowercase
>>> pairs = [''.join([a,b]) for a in letters for b in letters]
>>> threes = [''.join([a,b,c]) for a in letters for b in letters for c in letters]
>>> fours = [''.join([a,b,c,d]) for a in letters for b in letters for c in letters for d in letters]
>>> words = list(letters) + pairs + threes + fours
>>> def wcmp(x,y):
... if len(x) < len(y):
... return -1
... elif len(x) > len(y):
... return 1
... else:
... return cmp(x,y)
...
>>> words.sort(cmp=wcmp)
>>> words[words.index('a'):words.index('bb')+1]
['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', 'aa',
'ab', 'ac', 'ad', 'ae', 'af', 'ag', 'ah', 'ai', 'aj', 'ak', 'al',
'am', 'an', 'ao', 'ap', 'aq', 'ar', 'as', 'at', 'au', 'av', 'aw',
'ax', 'ay', 'az', 'ba', 'bb']
Note that this is not very practical if your words can get out to five
characters..
--
John.
More information about the Tutor
mailing list