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

2009-03-18 Thread Kent Johnson
On Wed, Mar 18, 2009 at 5:26 PM, Alexander Daychilde (Gmail)
 wrote:

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

This could be a little cleaner using tuple assignment to get rid of
the subscripting and list comprehension to get rid of the loop:

lower, upper = exp.split(':')
min_padding = len(lower)
exp_list = [ '%0*d' % (min_padding, i) for in in range(int(lower),
int(upper)+1) ]

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2009-03-18 Thread John Fouhy
2009/3/19 Alexander Daychilde (Gmail) :
> 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.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


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

2009-03-18 Thread Wayne Watson
Title: Signature.html




It looks like you used the wrong thread to post this. It's connected to
questions about linux and Win, and not iterations.

Alexander Daychilde (Gmail) wrote:

  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)

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

  


-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)


“Life is one damn thing after another."
 -- Mark Twain 




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor