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


Groups > comp.lang.python > #82847

learning to use iterators

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!news.unit0.net!takemy.news.telefonica.de!telefonica.de!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'python,': 0.02; 'indices': 0.07; 'none)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; '+---': 0.16; 'footnotes': 0.16; 'parentheses': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'there?': 0.16; 'thanks,': 0.17; 'trying': 0.19; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'fairly': 0.24; 'header:X-Complaints- To:1': 0.27; "i'm": 0.30; 'piece': 0.31; 'subject:learning': 0.31; 'tuples': 0.31; 'quite': 0.32; 'hi,': 0.36; 'list': 0.37; 'implement': 0.38; 'window': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'helps': 0.61; 'new': 0.61; 'first': 0.61; 'great': 0.65; 'received:130': 0.73; 'n):': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Seb <spluque@gmail.com>
Subject learning to use iterators
Date Tue, 23 Dec 2014 12:55:13 -0600
Organization Church of Emacs
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host net82.ceos.umanitoba.ca
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)
Cancel-Lock sha1:KayWRL8EyefZJaSHN2rHQYdh4uU=
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17158.1419360928.18130.python-list@python.org> (permalink)
Lines 25
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1419360928 news.xs4all.nl 2939 [2001:888:2000:d::a6]:34954
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:82847

Show key headers only | View raw


Hi,

I'm fairly new to Python, and while trying to implement a custom sliding
window operation for a pandas Series, I came across a great piece of
code¹:

>>> def n_grams(a, n):
...     z = (islice(a, i, None) for i in range(n))
...     return zip(*z)
...

I'm impressed at how succinctly this islice helps to build a list of
tuples with indices for all the required windows.  However, I'm not
quite following what goes on in the first line of the function.
Particulary, what do the parentheses do there?

Thanks,


+--- Footnotes ---+
¹ http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html#sliding-windows-n-grams-using-zip-and-iterators

-- 
Seb

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

learning to use iterators Seb <spluque@gmail.com> - 2014-12-23 12:55 -0600

csiph-web