Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nntp-feed.chiark.greenend.org.uk!ewrotcd!news.nosignal.org!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'string.': 0.04; 'that?': 0.05; 'subject:help': 0.07; 'python': 0.09; 'minus': 0.09; 'subject:skip:m 10': 0.09; 'subject:string': 0.09; 'sat,': 0.15; '7:35': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'sign,': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'followed': 0.20; 'assuming': 0.22; 'split': 0.23; 'header:In- Reply-To:1': 0.25; 'skip:" 20': 0.26; 'message- id:@mail.gmail.com': 0.27; 'that.': 0.30; 'skip:s 30': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'skip:. 20': 0.35; 'pm,': 0.35; 'received:209.85.220': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'characters': 0.36; 'two': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'skip:( 30': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'remove': 0.61; 'subject:Need': 0.61; 'back': 0.62; 'subject:. ': 0.66; 'as:': 0.75; 'guaranteed': 0.76; '2013': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=UUE2KVHQLQjeIO00ET2Bm5C/nwZM8nPCgUT5jZaHRiM=; b=LdhMM6zScc+arRgdgWohMVnCWKRqRH0F5ndlk684C5ujPaFOmhmmUHCVc56vY0a3aF iRl9biKH+chWPzwSCmdYCcWTIDQwERT18RUWyP8vq3iE1grFPBVrLlj8xlQadfuNsuIz 8I/jFM5ooOmRFbbcDB/Xz4yqFMjf9UZKFzwoM77yfdnoIBbDNU0dR8WVDlcQKN2F7XxA WaR3HzRQH8yTA9UCQ3Jr6MyNHkH+k3UVoxMzYz1Q1/Pkpw5ZY6nBqfhXxhH8gffBY+AE o05Rhk9aviVsFfb7ahCY2eJuaCQ/gx3vu0yUx0lPrStYncEcymuYzKy5t5XmUXm8GPx1 qDYQ== MIME-Version: 1.0 In-Reply-To: References: Date: Sat, 5 Jan 2013 20:27:46 +1100 Subject: Re: Need a specific sort of string modification. Can someone help? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1357378077 news.xs4all.nl 6950 [2001:888:2000:d::a6]:52601 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36155 On Sat, Jan 5, 2013 at 7:35 PM, 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? 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: s=".+3ACG.+5CAACG.+3ACG.+3ACG" result = "".join([x[int(x[0])+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. ChrisA