Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'string.': 0.04; 'that?': 0.05; 'elegant': 0.07; 'subject:help': 0.07; 'python': 0.09; 'minus': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:m 10': 0.09; 'subject:string': 0.09; 'def': 0.10; 'dots': 0.16; 'max:': 0.16; 'naive': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sign,': 0.16; 'wrote:': 0.17; 'char': 0.17; 'followed': 0.20; 'thanks.': 0.21; 'pos': 0.22; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'possibility': 0.27; 'correct': 0.28; 'header:X -Complaints-To:1': 0.28; 'that.': 0.30; 'could': 0.32; 'to:addr :python-list': 0.33; 'skip:. 20': 0.35; 'subject:?': 0.35; 'there': 0.35; 'received:org': 0.36; 'except': 0.36; 'characters': 0.36; 'two': 0.37; 'received:co.za': 0.37; 'received:za': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'remove': 0.61; 'subject:Need': 0.61; 'more': 0.63; 'here': 0.65; 'subject:. ': 0.66; 'received:41': 0.73; 'as:': 0.75; 'frank': 0.75; 'caters': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Frank Millman Subject: Re: Need a specific sort of string modification. Can someone help? Date: Sat, 05 Jan 2013 11:15:59 +0200 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 41-135-111-135.dsl.mweb.co.za User-Agent: Mozilla/5.0 (Windows NT 5.2; rv:14.0) Gecko/20120713 Thunderbird/14.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1357377381 news.xs4all.nl 6888 [2001:888:2000:d::a6]:49600 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36154 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