Re: Pythonic love
justin walters writes: > Sorry about the top posting. I'm new to mailing lists. I should just reply > to the python-list@python.org address then? > > Also, thank you for the generator clarification. > On Mar 8, 2016 9:09 AM, "jmp" wrote: ^ that's top posting always reply inline or at the bottom -- https://mail.python.org/mailman/listinfo/python-list
Re: Pythonic love
Sorry about the top posting. I'm new to mailing lists. I should just reply to the python-list@python.org address then? Also, thank you for the generator clarification. On Mar 8, 2016 9:09 AM, "jmp" wrote: > On 03/08/2016 05:49 PM, justin walters wrote: > >> Correct me if I'm wrong, but don't python generators usually use the yield >> statement so they can be used in list comprehensions? >> > > Hello, > > Please don't top post. > > Generator expressions are different from generator functions. They are > both generators but use a different syntax. > > generator function: > > def func(iterable): > for i in iterable: yield i > > > the generator expression version would be > > (i for i in iterable) > > > Both have their use cases but everytime you can actually use an generator > expression, it's probably the correct thing to do. > > Cheers, > > jm > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Pythonic love
On 03/08/2016 05:49 PM, justin walters wrote: Correct me if I'm wrong, but don't python generators usually use the yield statement so they can be used in list comprehensions? Hello, Please don't top post. Generator expressions are different from generator functions. They are both generators but use a different syntax. generator function: def func(iterable): for i in iterable: yield i the generator expression version would be (i for i in iterable) Both have their use cases but everytime you can actually use an generator expression, it's probably the correct thing to do. Cheers, jm -- https://mail.python.org/mailman/listinfo/python-list
Re: Pythonic love
On 08/03/2016 16:49, justin walters wrote: Correct me if I'm wrong, but don't python generators usually use the yield statement so they can be used in list comprehensions? Please don't top post on this list, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: Pythonic love
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 > -- https://mail.python.org/mailman/listinfo/python-list
Re: Pythonic love
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
Re: Pythonic love
On 07/03/2016 22:51, 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) Something like this, completely untested. with open(prfile,mode="rt",encoding='utf-8') as pfile: lineIter = iter(pfile) for _ in range(5): next(lineIter) for line in lineIter: allVals = line.strip().split("\t") print(allVals) I'd also recommend that you use the csv module https://docs.python.org/3/library/csv.html which can also cope with tsv files, it's far more reliable than relying on a simple call to split(). -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: Pythonic love
On 3/7/2016 6:03 PM, sohcahto...@gmail.com wrote: On a side note, your "with open..." line uses inconsistent quoting. > You have "" on one string, but '' on another. Thanks. I'll make sure I flog myself three times later tonight... -- https://mail.python.org/mailman/listinfo/python-list
Re: Pythonic love
On Mon, Mar 7, 2016 at 3: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 I'd probably use itertools.islice. from itertools import islice for line in isiice(pfile, 5, None): allVals = line.strip().split("\t") print(allVals) -- https://mail.python.org/mailman/listinfo/python-list
Re: Pythonic love
On Monday, March 7, 2016 at 2:51:50 PM UTC-8, 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) I'd read all the lines at once and then just slice the list. with open(prfile, mode="rt", encoding="utf-8") as pfile: lines = pfile.readlines()[5:] for line in lines: allVals = line.strip().split("\t") print(allVals) Obviously, this will only work well if your file is a reasonable size, as it will store the entire thing in memory at once. On a side note, your "with open..." line uses inconsistent quoting. You have "" on one string, but '' on another. -- https://mail.python.org/mailman/listinfo/python-list
Re: Pythonic love
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) for lineno, line in enumerate(pfile, start=1): will save you maintaining your own counter. But other than that, nah, that's pretty much the approach you're looking for. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. -- https://mail.python.org/mailman/listinfo/python-list