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


Groups > comp.lang.python > #9470

Re: Suppressing newline writing to file after variable

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'responding': 0.04; 'function,': 0.07; 'statements,': 0.07; 'python': 0.08; "(i'm": 0.09; 'loop.': 0.09; 'pos': 0.09; 'am,': 0.13; 'subject:file': 0.13; 'wrote:': 0.15; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "function's": 0.16; 'return.': 0.16; 'skip:r 50': 0.16; 'sorting': 0.16; 'subject:variable': 0.16; 'subject:writing': 0.16; 'edward': 0.16; '>>>': 0.16; 'def': 0.16; 'received:209.85.210.174': 0.19; 'received:mail-iy0-f174.google.com': 0.19; 'convert': 0.19; 'figure': 0.21; 'header:In-Reply-To:1': 0.22; 'trying': 0.23; 'builds': 0.23; 'enhancements': 0.23; 'long.': 0.23; 'code': 0.24; 'skip:l 30': 0.25; 'skip:[ 10': 0.26; 'function': 0.26; 'hey': 0.26; "i'm": 0.27; 'work.': 0.28; 'sort': 0.28; 'putting': 0.28; 'fri,': 0.28; 'message-id:@mail.gmail.com': 0.28; 'idle': 0.29; 'second': 0.29; 'example': 0.30; 'definition': 0.30; 'turned': 0.30; 'grasp': 0.30; 'logic': 0.30; 'version': 0.30; 'collecting': 0.31; 'chris': 0.32; "skip:' 10": 0.32; 'list': 0.32; 'does': 0.32; "i'll": 0.33; 'it.': 0.33; 'actually': 0.33; 'to:addr :python-list': 0.34; 'post': 0.34; 'test': 0.34; 'starting': 0.35; 'that,': 0.35; 'defining': 0.35; 'improvements': 0.35; 'thank': 0.35; '(with': 0.35; 'explain': 0.36; 'functions.': 0.37; 'things,': 0.37; 'some': 0.37; 'but': 0.37; 'could': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'takes': 0.38; 'subject:: ': 0.38; 'two': 0.38; 'put': 0.38; 'list,': 0.39; 'skip:s 20': 0.39; 'to:addr:python.org': 0.39; 'format': 0.40; 'received:209': 0.40; "i'd": 0.40; 'where': 0.40; 'hope': 0.60; 'your': 0.60; 'more.': 0.61; 'matter': 0.61; 'piece': 0.62; 'body': 0.63; 'believe': 0.66; 'chain': 0.66; 'wish': 0.70; 'order,': 0.73; 'compare:': 0.84; 'step.': 0.91; 'step,': 0.93
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=VNYp20BKCEtwrFAwOpH0+JW0/kmkjEukWho1biuNUoE=; b=m3aXw5cEePN6mz/GenTJ05m78cKiJJ3Cx+LLJjhITsqU0uGxfoDJEbyU/4sPrQPO0m TCyjvaZykBLlzB7+6EBlOk0qbA60i5quCxvC9++3mvSFNYs+bxQYUQqW96/PS2dFkqsR vpCRHjno/YXyn0s7hX+AwtZlQ5qO88Ttn58z8=
MIME-Version 1.0
In-Reply-To <77AE044B1BF3944FAE2435F395F11B4B01859B08@clt-exmb02.bbtnet.com>
References <77AE044B1BF3944FAE2435F395F11B4B016A2910@clt-exmb02.bbtnet.com> <BANLkTikpXEpgjvVsVWocw5wVVqnLmQDa5A@mail.gmail.com> <77AE044B1BF3944FAE2435F395F11B4B016A29A2@clt-exmb02.bbtnet.com> <BANLkTi=rn3Z5crEYG6VDUim=5aWgaADHRQ@mail.gmail.com> <77AE044B1BF3944FAE2435F395F11B4B01859B08@clt-exmb02.bbtnet.com>
Date Fri, 15 Jul 2011 00:38:32 +1000
Subject Re: Suppressing newline writing to file after variable
From Chris Angelico <rosuav@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1026.1310654316.1164.python-list@python.org> (permalink)
Lines 138
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1310654316 news.xs4all.nl 23885 [2001:888:2000:d::a6]:53053
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:9470

Show key headers only | View raw


On Fri, Jul 15, 2011 at 12:04 AM, Ellerbee, Edward <EEllerbee@bbandt.com> wrote:
> Hey Chris,
>
> I was reading over this again, trying to understand the logic (I'm a
> n00b)
>
> Could you explain this a bit? I'd like to build this emit function, but
> I still don't have a firm grasp on functions. All of my code is line by
> line. I'll convert it as a learn more.

I'm responding on-list as I believe others will wish to weigh in (if
only to point out some improvements to my code - it's hastily put
together). Caution, post is long.

Defining functions in Python is, broadly speaking, just a matter of
collecting up a bunch of statements and giving it a name. Compare:

if x>5:
   do_this()
   do_that()
   etc()

with:

def abcde():
   do_this()
   do_that()
   etc()

The first one does the three statements, in order, if and only if the
condition is true. The second one gives a name to those three
statements, so you can use it as a new statement:

abcde()

It'll do the same three things, every time you call it. It's
effectively the same as putting the function's body in where you call
it (that's an extremely sloppy explanation, but near enough).

> So, I'd want this to go after the sort step, and before the format step.
> I can figure that piece out, just trying to get this little block of
> code to work.

Yes, that would be the place to put it.

Here's a reworked version that you can test in IDLE:


def combine(list_of_numbers, position):
 lastprefix=tails=lastsuffix=None
 result=[]
 for cur in list_of_numbers:
  prefix=cur[:position]; tail=cur[position]; suffix=cur[position+1:]
  if prefix!=lastprefix or suffix!=lastsuffix:
   if lastprefix!=None:
    if len(tails)>1: result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix))
    else: result.append(lastprefix+tails+lastsuffix)
   lastprefix,tails,lastsuffix=prefix,"",suffix
  tails+=tail
 if lastprefix!=None:
  if len(tails)>1: result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix))
  else: result.append(lastprefix+tails+lastsuffix)
 return result

It incorporates some of the enhancements I mentioned in the original post.

>>> combine(['252205','252206','252208'],5)
['25220[568]']
>>> combine(['252205','252215','252225'],4)
['2522[012]5']

Notice that the 'emit' function is now 'result.append()' - it builds
up a list to return. You can now chain the calls; start with a list of
numbers and then call combine() in a loop.

# List of numbers from your previous post
numbers = ['252205', '252206', '252208', '252220', '252221', '252222',
'252223', '919745', '919725', '919785', '704770', '704771', '704772',
'704773', '704774', '704775', '704776', '704777', '704778', '704779',
'704780', '704781', '704782', '704783', '704784', '704785', '704786',
'704787', '704788', '704789', '704790', '704791', '704792', '704793',
'704794', '704795', '704796', '704797', '704798', '704799']


numbers = combine(numbers,5)

numbers = combine(numbers,4)

numbers = combine(numbers,3)

numbers = combine(numbers,2)

numbers = combine(numbers,1)

numbers = combine(numbers,0)

If you do these statements one at a time in IDLE and inspect the
'numbers' list each time, you'll see the combinations sorting
themselves out. (With this starting list, only the first two will have
any effect.)

The last set of calls can be turned into a for loop:
for pos in range(5,-1,-1):
   numbers = combine(numbers,pos)

In fact, you could actually take it out of being a function, if you wanted to:

list_of_numbers = ['252205', '252206', '252208', '252220', '252221',
'252222', '252223', '919745', '919725', '919785', '704770', '704771',
'704772', '704773', '704774', '704775', '704776', '704777', '704778',
'704779', '704780', '704781', '704782', '704783', '704784', '704785',
'704786', '704787', '704788', '704789', '704790', '704791', '704792',
'704793', '704794', '704795', '704796', '704797', '704798', '704799']

for position in range(5,-1,-1):
 lastprefix=tails=lastsuffix=None
 result=[]
 for cur in list_of_numbers:
  prefix=cur[:position]; tail=cur[position]; suffix=cur[position+1:]
  if prefix!=lastprefix or suffix!=lastsuffix:
   if lastprefix!=None:
    if len(tails)>1: result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix))
    else: result.append(lastprefix+tails+lastsuffix)
   lastprefix,tails,lastsuffix=prefix,"",suffix
  tails+=tail
 if lastprefix!=None:
  if len(tails)>1: result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix))
  else: result.append(lastprefix+tails+lastsuffix)
 list_of_numbers = result

That's what the function definition does - it takes a block of code
and gives it a new name. (There's a lot more to it than that, thank
you pedants I know, but in this simple example that's what it's
doing.)

Hope that's of use!

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-07-15 00:38 +1000

csiph-web