473,574 Members | 2,203 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Incrementing a string

I've used perl for a lot of 'throw away' scripts; I like Python better in
principle, from reading about it, but it was always easier to just use
perl rather than learn python.

Now I'm writing a smallish program that I expect to keep around, so am
taking this opportunity to try to learn some Python. I have a need for
computer generated set of simple string lables. I don't know how many in
advance---each is produced as a result of a user action.

In perl I simply initiated

$label= "a";

Then, after using it doing

$label++;

This conveniently steps through the alphabet, then goes on to aa, ab,ac,
....

In Python I can get from a to z with a generator as so:
def gen_alph(): .... for i in range(97,123):
.... yield chr(i)
.... g = gen_alph()
g.next() 'a' g.next() 'b' g.next()

'c'

But it looks like going beyond z to aa and so on is (relatively) complicated.

In truth, it seems unlikely that I would ever go beyond z in using my
application, and certainly not beyond zz which wouldn't be too hard to
program. But I hate to build in limitations no matter how reasonable.

It seems like there should be a better way that I'm missing because I'm
thinking in perl, not thinking in Python. :-)

Best,

John Velman
Jul 18 '05 #1
11 4729
John Velman <ve****@cox.net > writes:
But it looks like going beyond z to aa and so on is (relatively)
complicated.

In truth, it seems unlikely that I would ever go beyond z in using my
application, and certainly not beyond zz which wouldn't be too hard to
program. But I hate to build in limitations no matter how reasonable.


Python doesn't have that feature built in, so you have to think about
how Perl really does it, and implement something similar in Python.
Yes, it will be a little bit complicated, but the Perl implementers
had to deal with the same complexity, and it's a decent exercise for
you to do the same.
Jul 18 '05 #2
[John Velman]:

[how do I implement "$a = 'z'; $a++;" in Python?]


is there any reason you need your labels to be on this format?
usually, I would find a plain "L42" more readable.

def gen_label():
num = 0
while True:
num += 1
yield "L" + str(num)
Perl's increment operator isn't a generator, it just changes the
current value so you can jump back and forth. here's one quick go at
it. I'm sure it can be done more prettily, and probably without
assuming the string is ASCII :-)

def incr(s):
i = 1
while i <= len(s):
if 'a' <= s[-i] < 'z':
return s[:-i] + chr(88 + int(s[-i], 36)) + s[len(s)+1-i:]
elif s[-i] == 'z':
s = s[:-i] + 'a' + s[len(s)+1-i:]
else:
raise ValueError
i += 1
return 'a' + s

--
Kjetil T.
Jul 18 '05 #3
#### begin sample program ####

import string

class LabelCounter(ob ject):
digits = string.lowercas e

def __init__(self, value=0):
self.value = value

def __str__(self):
letters = []
i = self.value
while i:
d, m = divmod(i, len(self.digits ))
letters.append( self.digits[m])
i = d
return ''.join(letters[::-1]) or self.digits[0]

def __add__(self, other):
return LabelCounter(se lf.value + other)

def __lt__(self, other):
return self.value < other

# define other operators as needed; it's a shame this can't inherit from
# int and get these for free. It can't do this because all of int's
# operators return ints, not LabelCounters.
i = LabelCounter(0)
while i < 50:
print i
i += 1

#### end sample program ####

You can set 'digits' to any sequence at all. Set it to '01' to get
output in binary, or to ['01','23','45', '67','89'] to get base 5 in a
very confusing notation *g*

On Wed, Sep 15, 2004 at 03:08:20PM -0700, John Velman wrote:
I've used perl for a lot of 'throw away' scripts; I like Python better in
principle, from reading about it, but it was always easier to just use
perl rather than learn python.

Now I'm writing a smallish program that I expect to keep around, so am
taking this opportunity to try to learn some Python. I have a need for
computer generated set of simple string lables. I don't know how many in
advance---each is produced as a result of a user action.

In perl I simply initiated

$label= "a";

Then, after using it doing

$label++;

This conveniently steps through the alphabet, then goes on to aa, ab,ac,
...

In Python I can get from a to z with a generator as so:
def gen_alph(): ... for i in range(97,123):
... yield chr(i)
... g = gen_alph()
g.next() 'a' g.next() 'b' g.next()

'c'

But it looks like going beyond z to aa and so on is (relatively) complicated.

In truth, it seems unlikely that I would ever go beyond z in using my
application, and certainly not beyond zz which wouldn't be too hard to
program. But I hate to build in limitations no matter how reasonable.

It seems like there should be a better way that I'm missing because I'm
thinking in perl, not thinking in Python. :-)

Best,

John Velman
--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #4
John Velman wrote:
$label++;

This conveniently steps through the alphabet, then goes on to aa, ab,ac,
...

In Python I can get from a to z with a generator as so: [snip] But it looks like going beyond z to aa and so on is (relatively)
complicated.


Here's one way, perhaps it is simple enough for you:

import string

def strpp(s):
for i in s:
yield i
for i in strpp(s):
for j in s:
yield i + j

labels = strpp(string.lo wercase)
label = label.next() # ad infinitum
Hope you enjoy Python,
Jeffrey
Jul 18 '05 #5
On Wed, 15 Sep 2004 15:08:20 -0700 John wrote:
This conveniently steps through the alphabet, then goes on to aa, ab,ac,


Puzzling... I hacked at this problem as a flexibility learning exercise
and actually got this to work; but... does 'while True' in a generator
have side-effects...?? Or generator recursion?

Maybe I'm just tired -- I don't see how the string actually _grows_ inside
the while.... :^?

I was just expecting the prefix to be '', then 'a'...'z' giving a..z,
aa..az, ba..bz, ... za..zz -- not continuing through aaa...azz and
onwards... It's cool; but boggles my mind at the moment... not that
that's a stretch... :^)

def ascinc(start='a ',end='z'):
g = ascinc(start,en d)
prefix = ''
while True:
for i in range(ord(start[-1]),ord(end[-1])+1):
yield (prefix + chr(i))
prefix = g.next()
print "what you wanted..."
g = ascinc('a')

for i in range(100):
print g.next(),

print
print "including some flexibility..."

g = ascinc('0','1')

for i in range(100):
print g.next(),
Jul 18 '05 #6
Here's my generator version:

def gen_alph():
def convert(k,n):
# convert integer k to n-letter string
out = []
for i in xrange(n):
k,r = divmod(k,26)
out.append(chr( 97+r))
out.reverse()
return ''.join(out)

nletters = 1
while True:
for k in xrange(26**nlet ters):
yield convert(k, nletters)
nletters += 1

To test it:

g = gen_alph()
for i in range(40):
print i, g.next()
Jul 18 '05 #7
Phil Frost wrote:
import string

class LabelCounter(ob ject):
digits = string.lowercas e .... # define other operators as needed; it's a shame this can't inherit from
# int and get these for free. It can't do this because all of int's
# operators return ints, not LabelCounters.
Try this instead. As usual with clever solutions, you
probably shouldn't use this in real code.

import string

def fixup(klass):
for name, meth in inspect.getmemb ers(klass):
# Find all the methods except for a few special ones
# we know won't return integers
if (callable(meth) and name not in
["__class__" , "__new__", "__init__", "__str__", "__repr__"]):

# Need to make a wrapper function (g) for each of these methods.
# The wrapper function needs to know the original method, which
# is stored in the newly created scope (f).
def f(meth = meth):
def g(self, *args, **kwargs):
retval = meth(self, *args, **kwargs)
if isinstance(retv al, int):
return LabelCounter(re tval)
return retval
return g
g = f()
setattr(klass, name, g) # replace with the wrapped version

class LabelCounter(in t):
digits = string.ascii_lo wercase
def __str__(self):
letters = []
i = int(self)
while i:
d, m = divmod(i, len(self.digits ))
letters.append( self.digits[m])
i = d
return "".join(let ters[::-1]) or self.digits[0]
__repr__ = __str__
Here it is in use.
a = LabelCounter(4)
print a e print a+1 f print a+10 o print (a+10)/2 h print a e print a/2 c print a/3 b print a/30 a print a**4 jw You can set 'digits' to any sequence at all. Set it to '01' to get
output in binary, or to ['01','23','45', '67','89'] to get base 5 in a
very confusing notation *g*


I set it to ascii_lowercase since string.lowercas e is locale
dependent.
import string, locale
string.lowercas e 'abcdefghijklmn opqrstuvwxyz' locale.setlocal e(locale.LC_ALL , "de_DE") 'de_DE' string.lowercas e 'abcdefghijklmn opqrstuvwxyz\xa a\xb5\xba\xdf\x e0\xe1\xe2\xe3\ xe4\xe5\xe6\xe7 \xe8\xe9\xea\xe b\xec\xed\xee\x ef\xf0\xf1\xf2\ xf3\xf4\xf5\xf6 \xf8\xf9\xfa\xf b\xfc\xfd\xfe\x ff'


OTOH, that might be what you wanted ...

Andrew
da***@dalkescie ntific.com
Jul 18 '05 #8
Jeffrey Froman wrote:
import string

def strpp(s):
for i in s:
yield i
for i in strpp(s):
for j in s:
yield i + j

labels = strpp(string.lo wercase)
label = label.next() # ad infinitum


string.ascii_lo wercase may be preferable here.
Jul 18 '05 #9
Thanks to everyone who responded! I've learned a good deal by reading
through the solutions. Don't know which one I'll use yet (or perhaps I'll
use something suggested by them), but these take me another step toward
'thinking in Python'. Also, I might add, it's really hard to know of all
the machinery that is available --- there is so much of it. I now know
about the itertools module, for example.

Kjetil Torgrim Homme asked why not just make the labels L1, L2, ..., Paul
Foley proposed similarly: label1, label2, ..., and also suggested that if
$label="one" it would make sense for $label++ to be "two".

Either of these would work (although "one","two" ,... would be yet a
different programming problem). I prefer my initial choice because the
labels will be used interactively, and I prefer to be able to do
everything from the keyboard. It's easier for me to type "aq" than "L42".
I'm a pretty good touch typist as far as lower case letters go, but
usually have to look at the keyboard to type numerals. So "m" is
preferable to "L12" but, to me, there isn't much difference between
typing "l12" and "label12".

I think some of the proposed solutions would make it easy to taylor the
kind of label as a user preference if I ever get this application to the
point that I think someone else might be interested in using it.

And yes, I think I'm going to enjoy Python.

Best,

John Velman
Jul 18 '05 #10

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

Similar topics

9
7213
by: Michael | last post by:
Hi, I've got a string s, and i want to shift all the letters up by one, eg a->b, b->c ........ z->a In c++ i can do this quite simply with if(C == 'z') C='a'; else C++; but i can't work out how to do this this in python??
3
2440
by: James Aguilar | last post by:
Hey all, I have a bit of code that implements a suffix array, which is essentially an array of iterators or indexes into a string. The array starts out like this String: ABAB Array: 0123 But then gets sorted
2
420
by: brian | last post by:
Hi, before coming to .NET, I utilized regular expressions mostly in JScript / JavaScript and also in my favorite text editor: TextPad (www.textpad.com) I don't know about JScript/JavaScript, but in TextPad's implementation of Regular Expressions, you can do a replacement expression like this: \i For every single non-overlapping match, it...
8
2377
by: mantrid | last post by:
Hello I have the following code, where clicking yh1r is supposed to move h1 10px down and update the value of yh1 by 20 each time its clicked. what the code actually does is NOT move h1 and instead of incrementing yh1 from 0 to 20 to 40 etc it doubles it and appends it to the original value each time eg 0 to 20 to 4020 to 804020 to 160804020...
1
3642
by: snareklutz | last post by:
Hi, I saw the thread here: http://www.thescripts.com/forum/thread255915.html and I am currently working on building on it. Well, what I need is something that takes in a string and increments it by one. eg. 0 becomes 1 1 becomes 2 9 becomes a a becomes b
4
1821
by: masterpaladin38 | last post by:
I've been working on this problem for some time and I must say I'm stump. Any help would be appreciated. Basically what I'm trying to do is write the results of a loop to a new text file with every pass. For example the user chooses that the loop should run 10 times. I want to write the results of each pass of the loop to a text file by...
7
1919
by: jwhitby3 | last post by:
Hi all, I am trying to develop what amounts to a data entry page for the company I work for, (mostly to make my job easier). I think that I am beginning to grasp php, but I am at a loss now. I understand how to use HTML_Table to add a table to a page, and that portion of my project is coming along nicely. The problem is at this point, that I...
1
1444
by: jesmi | last post by:
hi i got problem in inserting the date into the database. my requirement is that when i choose a date ie from :2007-01-01 & to :2007-12-01 then all the dates starting from 2007-02-01 upto 2007-12-01 should be inserted. while inserting year,month and day should be incremented.i tried a lot and my code only increments the month. Following is my...
3
1264
by: =?Utf-8?B?TWlrZSBL?= | last post by:
I have a Net 2.0 app in VB that has code like this: Dim intA as Integer Dim intB as Integer dim myList as new List(Of String) intA = 10 intB = 20 .... code to fill the list ....
9
3355
by: subramanian100in | last post by:
The following portion is from c-faq.com - comp.lang.c FAQ list · Question 6.13 int a1 = {0, 1, 2}; int a2 = {{3, 4, 5}, {6, 7, 8}}; int *ip; /* pointer to int */ int (*ap); /* pointer to array of int */\ ap = &a1; printf("%d\n", **ap);
0
7803
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...
0
8044
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. ...
0
8230
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...
1
7810
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...
0
8096
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...
0
6451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3739
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...
1
2240
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1337
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.