Need a specific sort of string modification. Can someone help?
rosuav at gmail.com
Sat Jan 5 10:27:46 CET 2013
On Sat, Jan 5, 2013 at 7:35 PM, Sia <hossein.asgharian at gmail.com> wrote:
> I have strings such as:
> 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:
> How can I do that?
Interesting. Are you guaranteed that there are no other plus or minus
signs? Is the number after the sign just one digit? Assuming the
answers to both are "yes", here's a one-liner:
result = "".join([x[int(x)+1:] for x in ("0"+s).replace("-","+").split("+")])
Split on either - or +, then trim off that many characters from each
(that's the list comp), then join them back into a string.
More information about the Python-list