Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8555

Re: Suppressing newline writing to file after variable

References <77AE044B1BF3944FAE2435F395F11B4B016A2910@clt-exmb02.bbtnet.com> <BANLkTikpXEpgjvVsVWocw5wVVqnLmQDa5A@mail.gmail.com> <77AE044B1BF3944FAE2435F395F11B4B016A29A2@clt-exmb02.bbtnet.com>
Date 2011-06-29 08:56 +1000
Subject Re: Suppressing newline writing to file after variable
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.488.1309301799.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Jun 29, 2011 at 3:32 AM, Ellerbee, Edward <EEllerbee@bbandt.com> 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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Suppressing newline writing to file after variable Chris Angelico <rosuav@gmail.com> - 2011-06-29 08:56 +1000

csiph-web