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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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