Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: justin walters Newsgroups: comp.lang.python Subject: Re: Pythonic love Date: Tue, 8 Mar 2016 08:49:43 -0800 Lines: 51 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de pS7ugQViP4L+p/ge0W0fJg69IZsNiTomx1UzXSMVZsZQ== Return-Path: 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; 'lines.': 0.07; 'cc:addr :python-list': 0.09; 'generators': 0.09; 'wrong,': 0.09; 'python': 0.10; 'things.': 0.15; '2016': 0.16; 'lambda': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:on': 0.16; '\xc2\xa0if': 0.16; 'wrote:': 0.16; 'memory': 0.17; '<': 0.18; 'skip': 0.18; 'skip:l 30': 0.18; '(not': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'cc:no real name:2**0': 0.22; 'am,': 0.23; 'slightly': 0.23; 'header:In-Reply-To:1': 0.24; 'wonder': 0.27; 'message-id:@mail.gmail.com': 0.27; 'yield': 0.27; 'correct': 0.28; 'function': 0.28; 'values': 0.28; 'skip:( 20': 0.28; 'perl': 0.29; "i'm": 0.30; 'print': 0.30; 'url:mailman': 0.30; 'statement': 0.32; 'url:python': 0.33; 'usually': 0.33; 'url:listinfo': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'skip:( 30': 0.35; 'could': 0.35; 'files,': 0.35; 'quite': 0.35; 'received:74.125.82': 0.35; 'but': 0.36; 'should': 0.36; 'url:org': 0.36; 'possible.': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'skip:& 10': 0.37; 'thanks': 0.37; 'things': 0.38; 'url:mail': 0.40; 'here.': 0.62; 'more': 0.63; 'mar': 0.65; 'skip:\xc2 10': 0.67 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=38OP8LGe4z97LJDEbFrNN7Pq2Z7QFYi3/9QUaikOPMw=; b=I6TVGVn//jmcD+8VSHQml9lC0ARTOiCaP078zR3o9u/R9659HSZ+5BJ93Y9sbC5+Dh 3lCGC5EoHwVnxXJjcnvPVgVfLWrBM5gHXi8Wx8Ayx5vR62t2WoBdNlzWYm9xrHKZqfIh V0KflOSqkd1dirsyUbqbHJTANsmMiHIA7ak++SiDr8ovz3Ko31UI1vUfYsSmtC2hRWa+ J1eeGBTvL3PZ6mj4ZjDUgVupTCW8MDA3iMGziXB1RxqXTFAIh7IHjTf0w3KKqZuUiWCE QDHa8gXE/qDNDWFEUpgwzC96x/8ap/1AwgldS56TYYKFO0HDobuUtLEJJWY4RifW4yiZ iFgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=38OP8LGe4z97LJDEbFrNN7Pq2Z7QFYi3/9QUaikOPMw=; b=QS0o0Z7pKDvbi3jCPFTPFdM42j7DlgGvAlBzIW7wyZlzujbgJG37RhqwdlX0aSWj4V ZFrCTCoE5h1LMRNCfZyddazWRoNoe8uIWhGR9qWpYy3K7nglsiIvNJDhS4+tIY+la9OE OtltHcYXTWwmiAp4mgEgp/Y2s9kxySQf8dICJk1/AtIoCEKH//bmNFktofv95HAZlWYA IyUBF/ARoE2C/Euxkqop0F119g9kDay5LO0eteqNAF/BNZYauCQyj7QGiKmbye9NVwf+ KG6vaafrbTX1jqXITzRjw3N3Offdr8xkGhVe2ajO55MY6ME2yT8Cx+bCiwJnMxagbZ0X Cnsw== X-Gm-Message-State: AD7BkJITe+jYfy36t/TmVw8NpBhAu7O0Hu8voVeQ4uCNHaUUDU8Pw6jJgEGCAoznKLh/by+e9IfIFUyPBNA5yQ== X-Received: by 10.194.115.8 with SMTP id jk8mr32454227wjb.136.1457455783994; Tue, 08 Mar 2016 08:49:43 -0800 (PST) In-Reply-To: X-Content-Filtered-By: Mailman/MimeDel 2.1.21 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104344 Correct me if I'm wrong, but don't python generators usually use the yield statement so they can be used in list comprehensions? On Mar 8, 2016 5:27 AM, "jmp" wrote: > On 03/07/2016 11:51 PM, Fillmore wrote: > >> >> learning Python from Perl here. Want to do things as Pythonicly as >> possible. >> >> I am reading a TSV, but need to skip the first 5 lines. The following >> works, but wonder if there's a more pythonc way to do things. Thanks >> >> ctr = 0 >> with open(prfile,mode="rt",encoding='utf-8') as pfile: >> for line in pfile: >> ctr += 1 >> >> if ctr < 5: >> continue >> >> allVals = line.strip().split("\t") >> print(allVals) >> > > what about a generator expression ? The (not so)new hype: > > with open(prfile,mode="rt",encoding='utf-8') as pfile: > for values in (l.strip().split("\t") for (i, l) in enumerate(pfile) if i > >=5): > print values > > slightly dense, could be better with a lambda function > > tovalues = lambda l: l.strip().split("\t") > with open(prfile,mode="rt",encoding='utf-8') as pfile: > for values in (tovalues(l) for (i, l) in enumerate(pfile) if i >=5): > print values > > > This should even work quite efficiently on big files, because I don't > thing no more than one line is in memory at a given time. > > jm > > > > > -- > https://mail.python.org/mailman/listinfo/python-list >