Need a specific sort of string modification. Can someone help?
Frank Millman
frank at chagford.com
Sat Jan 5 04:15:59 EST 2013
On 05/01/2013 10:35, 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.
>
Here is a naive solution (I am sure there are more elegant ones) -
def strip(old_string):
new_string = ''
max = len(old_string)
pos = 0
while pos < max:
char = old_string[pos]
if char in ('-', '+'):
num_pos = pos+1
num_str = ''
while old_string[num_pos].isdigit():
num_str += old_string[num_pos]
num_pos += 1
pos = num_pos + int(num_str)
else:
new_string += old_string[pos]
pos += 1
return new_string
It caters for the possibility that the number following the +/- could be
greater than 9 - I don't know if you need that.
It works with your examples, except that the second one returns '....',
which I think is correct - there are 4 dots in the original string.
HTH
Frank Millman
More information about the Python-list
mailing list