Hi Sia, Here's another variation. It's within my tolerance for readability :-) and also quick, if that's an issue. It does 100,000 of your longer string in a couple of seconds on my venerable laptop.
It handles only single-digit numbers. For multi-digit, I'd be inclined to have a look at takewhile and/or dropwhile, both in itertools (Python 2.7 and 3.x only.) from string import maketrans class redux: def __init__(self): intab = '+-' outtab = ' ' # two spaces self.trantab = maketrans(intab, outtab) def reduce_plusminus(self, s): list_form = [r[int(r[0]) + 1:] if r[0].isdigit() else r for r in s.translate(self.trantab).split()] return ''.join(list_form) if __name__ == "__main__": p = redux() print p.reduce_plusminus(".+3ACG.+5CAACG.+3ACG.+3ACG") print p.reduce_plusminus("tA.-2AG.-2AG,-2ag") for n in range(100000): p.reduce_plusminus(".+3ACG.+5CAACG.+3ACG.+3ACG") On Saturday, 5 January 2013 19:35:26 UTC+11, Sia wrote: > I have strings such as: > > > > tA.-2AG.-2AG,-2ag > > or > > .+3ACG.+5CAACG.+3ACG.+3ACG > > > > The plus and minus signs are always followed by a number (say, i). I want > python to find each single plus or minus, remove the sign, the number after > it and remove i characters after that. So the two strings above become: > > > > tA.., > > and > > ... > > > > How can I do that? > > Thanks. -- http://mail.python.org/mailman/listinfo/python-list