Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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; 'else:': 0.03; 'responding': 0.04; 'function,': 0.07; 'statements,': 0.07; 'python': 0.08; "(i'm": 0.09; 'loop.': 0.09; 'newline': 0.09; 'pos': 0.09; 'message-----': 0.12; 'am,': 0.13; 'subject:file': 0.13; 'wrote:': 0.15; "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; 'convert': 0.19; 'to:2**1': 0.21; 'figure': 0.21; 'variable': 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; 'url:mailman': 0.27; 'work.': 0.28; 'sort': 0.28; 'putting': 0.28; 'fri,': 0.28; 'idle': 0.29; 'engineering': 0.29; 'second': 0.29; 'example': 0.30; 'subject:': 0.30; 'definition': 0.30; 'turned': 0.30; 'grasp': 0.30; 'logic': 0.30; 'version': 0.30; 'thanks': 0.31; 'collecting': 0.31; 'url:listinfo': 0.32; '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; 'sent:': 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; 'tasks': 0.35; 'from:': 0.36; 'charset:us-ascii': 0.36; 'explain': 0.36; 'file': 0.36; 'url:python': 0.37; 'functions.': 0.37; 'things,': 0.37; 'thursday,': 0.37; 'some': 0.37; 'but': 0.37; 'could': 0.37; 'takes': 0.38; 'url:org': 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; "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; 'july': 0.64; 'believe': 0.66; 'chain': 0.66; 'voice': 0.69; 'wish': 0.70; 'order,': 0.73; '10:39': 0.84; 'compare:': 0.84; 'step.': 0.91; 'step,': 0.93 X-IronPort-AV: E=Sophos;i="4.65,529,1304308800"; d="scan'208";a="42955615" X-Server-Uuid: 7C9F3A3D-0A58-4F63-8FA5-251C0509CB4B X-MimeOLE: Produced By Microsoft Exchange V6.5 Content-class: urn:content-classes:message MIME-Version: 1.0 Subject: RE: Suppressing newline writing to file after variable Date: Thu, 14 Jul 2011 10:53:23 -0400 In-Reply-To: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Suppressing newline writing to file after variable Thread-Index: AcxCNCzZFAh4UZPfT+63cQo9fq8Y9AAAW8NA References: <77AE044B1BF3944FAE2435F395F11B4B016A2910@clt-exmb02.bbtnet.com><77AE044B1BF3944FAE2435F395F11B4B016A29A2@clt-exmb02.bbtnet.com><77AE044B1BF3944FAE2435F395F11B4B01859B08@clt-exmb02.bbtnet.com> From: "Ellerbee, Edward" To: "Chris Angelico" , python-list@python.org X-OriginalArrivalTime: 14 Jul 2011 14:53:23.0458 (UTC) FILETIME=[C77ED620:01CC4235] X-CFilter-Loop: Reflected X-WSS-ID: 6201DBE21ZO1906256-01-01 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable 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: 171 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1310655219 news.xs4all.nl 23878 [2001:888:2000:d::a6]:44275 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:9471 Holy cow, that's perfect! Thanks so much :)=20 Would it be alright if I post my code on the list for critiquing? I'm learning python to supplement my voice engineering position - time consuming tasks that can be automated will take less time. Edward Ellerbee -----Original Message----- From: python-list-bounces+eellerbee=3Dbbandt.com@python.org [mailto:python-list-bounces+eellerbee=3Dbbandt.com@python.org] On Behalf Of Chris Angelico Sent: Thursday, July 14, 2011 10:39 AM To: python-list@python.org Subject: Re: Suppressing newline writing to file after variable On Fri, Jul 15, 2011 at 12:04 AM, Ellerbee, Edward 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,=20 > but I still don't have a firm grasp on functions. All of my code is=20 > 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=20 > 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=3Dtails=3Dlastsuffix=3DNone result=3D[] for cur in list_of_numbers: prefix=3Dcur[:position]; tail=3Dcur[position]; = suffix=3Dcur[position+1:] if prefix!=3Dlastprefix or suffix!=3Dlastsuffix: if lastprefix!=3DNone: if len(tails)>1: result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix)) else: result.append(lastprefix+tails+lastsuffix) lastprefix,tails,lastsuffix=3Dprefix,"",suffix tails+=3Dtail if lastprefix!=3DNone: 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 =3D ['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 =3D combine(numbers,5) numbers =3D combine(numbers,4) numbers =3D combine(numbers,3) numbers =3D combine(numbers,2) numbers =3D combine(numbers,1) numbers =3D 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 =3D combine(numbers,pos) In fact, you could actually take it out of being a function, if you wanted to: list_of_numbers =3D ['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=3Dtails=3Dlastsuffix=3DNone result=3D[] for cur in list_of_numbers: prefix=3Dcur[:position]; tail=3Dcur[position]; = suffix=3Dcur[position+1:] if prefix!=3Dlastprefix or suffix!=3Dlastsuffix: if lastprefix!=3DNone: if len(tails)>1: result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix)) else: result.append(lastprefix+tails+lastsuffix) lastprefix,tails,lastsuffix=3Dprefix,"",suffix tails+=3Dtail if lastprefix!=3DNone: if len(tails)>1: result.append("%s[%s]%s"%(lastprefix,tails,lastsuffix)) else: result.append(lastprefix+tails+lastsuffix) list_of_numbers =3D 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 -- http://mail.python.org/mailman/listinfo/python-list