Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'prefix': 0.09; 'wed,': 0.12; 'am,': 0.13; 'received:209.85.214.174': 0.13; 'received:mail-iw0-f174.google.com': 0.13; 'subject:file': 0.13; 'wrote:': 0.15; 'condense': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'list.)': 0.16; 'passes,': 0.16; 'subject:variable': 0.16; 'subject:writing': 0.16; 'suffix': 0.16; 'edward': 0.16; 'intermediate': 0.16; 'subsequent': 0.16; '(which': 0.20; 'header:In-Reply-To:1': 0.22; 'function': 0.26; 'pass': 0.28; 'received:209.85.214': 0.28; 'message- id:@mail.gmail.com': 0.28; 'tail': 0.30; 'chris': 0.32; 'list': 0.32; 'list.': 0.33; 'to:addr:python-list': 0.34; 'probably': 0.35; 'assuming': 0.37; 'but': 0.37; 'received:google.com': 0.38; 'not,': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'problem.': 0.38; 'something': 0.38; 'else': 0.38; 'should': 0.39; 'feed': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.40; "i'd": 0.40; 'results': 0.62; 'code):': 0.84; 'wildcard': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=fvpXzfNefffQvRQW1/+1hessmLMnFBFks9cCAA4876M=; b=gaYNFxT+abGQ8mReFGG7NwGiyt8eEXXWOblOgXzKRy+DqxLMFNxNouYk1yhWK++3Ne p8CUBu2b0RM26RW+N4DvccDy46CEdnh+kmI7hCjgQb3Czimbo7JBxp8tDSH+oz8KpRT3 izit+oX/1qeLXh6fericVH+SEzEmX+3DQHNXU= MIME-Version: 1.0 In-Reply-To: <77AE044B1BF3944FAE2435F395F11B4B016A29A2@clt-exmb02.bbtnet.com> References: <77AE044B1BF3944FAE2435F395F11B4B016A2910@clt-exmb02.bbtnet.com> <77AE044B1BF3944FAE2435F395F11B4B016A29A2@clt-exmb02.bbtnet.com> Date: Wed, 29 Jun 2011 08:56:35 +1000 Subject: Re: Suppressing newline writing to file after variable 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.12 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1309301800 news6.xs4all.nl 4363 [2001:888:2000:d::a6]:49367 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8555 On Wed, Jun 29, 2011 at 3:32 AM, Ellerbee, Edward wrote: > How to condense a group of numbers to a wildcard list. For example: > > 252205 > 252206 > 252208 > > Condense to: > 25220[568] Assuming you have the list sorted (if not, that's the first place to start), I'd do a six-pass approach - one pass for each digit. It's simple and readable, and I doubt you'll be working with enough numbers for its inefficiency to be a problem. Something like this (untested code): lastprefix=tails=None for cur in list_of_numbers: prefix=cur[:5]; tail=cur[5] if prefix!=lastprefix: if lastprefix!=None: if len(tails)>1: emit("%s[%s]"%(lastprefix,tails)) else emit(lastprefix+tails) lastprefix,tails=prefix,"" tails+=tail if lastprefix!=None: if len(tails)>1: emit("%s[%s]"%(lastprefix,tails)) else emit(lastprefix+tails) Feed the emitted results from this pass into the next pass, in which prefix is cur[:4] and tail is cur[4], etc. For the subsequent passes, you'll also need to worry about a suffix (which would be cur[5:] when you're looking at cur[4]), and ensure that that matches, same as the prefix. This is completely untested, but it should be a start. (Replace the calls to the imaginary "emit" function with whatever you do to emit results - for the intermediate passes, that's probably appending to a list.) Chris Angelico