Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'indices': 0.07; 'none)': 0.09; 'received:151': 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; 'lambda': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'header:X-Complaints- To:1': 0.27; "i'm": 0.30; '>>>>': 0.31; 'subject:learning': 0.31; 'tuples': 0.31; 'there': 0.35; 'received:it': 0.37; 'list': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'helps': 0.61; 'n):': 0.84; 'received:libero.it': 0.84; 'theme:': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Vito De Tullio Subject: Re: learning to use iterators Date: Wed, 24 Dec 2014 16:12:01 +0100 References: <878uhyb3hq.fsf@net82.ceos.umanitoba.ca> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: ppp-224-3.26-151.libero.it User-Agent: KNode/4.14.3 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: 18 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1419433939 news.xs4all.nl 2911 [2001:888:2000:d::a6]:54949 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82892 Seb wrote: >>>> 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. If you want it succinctly, there is this variation on the theme: n_grams = lambda a, n: zip(*(a[i:] for i in range(n))) -- By ZeD