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


Groups > comp.lang.python > #8555 > unrolled thread

Re: Suppressing newline writing to file after variable

Started byChris Angelico <rosuav@gmail.com>
First post2011-06-29 08:56 +1000
Last post2011-06-29 08:56 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#8555 — Re: Suppressing newline writing to file after variable

FromChris Angelico <rosuav@gmail.com>
Date2011-06-29 08:56 +1000
SubjectRe: Suppressing newline writing to file after variable
Message-ID<mailman.488.1309301799.1164.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web