[Tutor] Iterating over letters or arbitrary symbols like they were numbers...

Alexander Daychilde (Gmail) daychilde at gmail.com
Wed Mar 18 22:26:52 CET 2009


If there's an easy way to do this, I'd like to have a pointer to it (i.e.
what functions would deal with this - not wanting my code written for me...)

Right now, I have written code to generate a list of strings that happen to
be a range of numbers. (The fact that they're strings is actually desirable
to me). My code looks at the range given to the function and zero-pads based
on the length of the start of the range. Ranges are indicated by
number-colon-number, e.g. 1:9 or 01:99 or 1:99. (for reasons outside this
snippet of code, I refer to these as "expressions" or "exp" for short...)

Here's the code in question:

______________

exp_list = []

exp_range = exp.split(":")

min_padding = len(exp_range[0])

for i in range(int(exp_range[0]),(int(exp_range[1])+1)):

    exp_list.append('%0*d' % (min_padding, i))

______________

(in fact, I'm *actually* parsing something like n(1:9;13;15;17:25) - so I
have multiple ranges and individual numbers to add to exp_list[], so in my
actual code, the list exists elsewhere, and this code is executed if I find
a colon in an element in the list created from splitting the original line
on commas (and removing the n and parentheses) - hope that makes sense)

I'm quite proud of that - for the level of programming I feel I'm at, I
thought it was somewhat clever. ;-) But I'm open to feedback on that... BUT,
here's what I need to do:

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

Am I stuck rolling my own arithmetic-type function? (i.e. z+a=aa and z+b=ab,
etc)

Thank you very much for any advice (and again, in addition to my actual
question, I wouldn't mind hearing if my solution for the numbers is less
clever than I thought-- i.e. not looking for praise; rather, looking for
improvement if it's glaringly dumb)



More information about the Tutor mailing list