Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'argument': 0.05; 'indices': 0.07; '[0,': 0.09; '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; 'jan': 0.12; 'expression.': 0.16; 'parentheses': 0.16; 'range(0,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'there?': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "i'm": 0.30; '>>>>': 0.31; 'omitted': 0.31; 'subject:learning': 0.31; 'tuples': 0.31; 'quite': 0.32; 'list': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'expression': 0.60; 'helps': 0.61; 'first': 0.61; 'n):': 0.84; 'received:fios.verizon.net': 0.84; 'received:108': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: learning to use iterators Date: Tue, 23 Dec 2014 15:46:27 -0500 References: <878uhyb3hq.fsf@net82.ceos.umanitoba.ca> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-108-16-203-145.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 In-Reply-To: <878uhyb3hq.fsf@net82.ceos.umanitoba.ca> 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: 23 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1419367616 news.xs4all.nl 2945 [2001:888:2000:d::a6]:34558 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82854 On 12/23/2014 1:55 PM, 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. However, I'm not > quite following what goes on in the first line of the function. > Particulary, what do the parentheses do there? The parenthesized expression is a generator expression. The ()s are part of the syntax and may only be omitted when the expression is the argument of a function call, as in >>> list((i for i in range(0, 7, 2))) [0, 2, 4, 6] >>> list(i for i in range(0, 7, 2)) [0, 2, 4, 6] -- Terry Jan Reedy