Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'python,': 0.02; 'indices': 0.07; 'none)': 0.09; 'def': 0.12; '23,': 0.16; 'expression,': 0.16; 'iterator,': 0.16; 'keyword.': 0.16; 'parentheses': 0.16; 'there?': 0.16; 'wrote:': 0.18; 'trying': 0.19; '>>>': 0.22; 'email addr:gmail.com>': 0.22; '>>>': 0.24; 'expanded': 0.24; 'specify': 0.24; 'fairly': 0.24; '>': 0.26; 'equivalent': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; '[1]': 0.29; 'am,': 0.29; 'dec': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'piece': 0.31; 'subject:learning': 0.31; 'tuples': 0.31; 'another': 0.32; 'quite': 0.32; 'url:python': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'building': 0.35; 'yield': 0.36; 'hi,': 0.36; 'similar': 0.36; 'url:org': 0.36; 'example,': 0.37; 'list': 0.37; 'list.': 0.37; 'implement': 0.38; 'window': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'expression': 0.60; 'helps': 0.61; 'new': 0.61; 'url:3': 0.61; 'first': 0.61; 'skip:n 10': 0.64; 'great': 0.65; 'n):': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=WwWXu8nQONN2yOC0AxHho3fd4RZHgLsY5SnebJ+mUJs=; b=Vq547gbSdeA0LNvx8j+ABdhpZkpNo3a0L/0tnA6u9Sl1OWwSdWBXPQnkELRyPIDEOh 1mO5F+4QcSjszuDpSeUCsXtmBUwfJPnPZ6JNIHMOVFERNLYBEV9ypuyHOA6AW/wvd5lr oda9lI3kCQGUIC0Ty9iG+QRzrPRWqPhTFQ764S/HxGoeB2Hcu1eHGIw29RUtIFzrTUpJ j3vyC+DUsWVEZewFQAiFDCxttryHGaDm6K4tEYuoGc5y82oqKyiH0RNf7i9/lacmgrQe slvWrpNlqF14QlsZ7p4UGZezWQUMR/7+9whC67ai16qS4tLQJdPHTQ7ASnSXy5kof9As fUIg== X-Received: by 10.67.14.129 with SMTP id fg1mr46268780pad.114.1419362665853; Tue, 23 Dec 2014 11:24:25 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <878uhyb3hq.fsf@net82.ceos.umanitoba.ca> References: <878uhyb3hq.fsf@net82.ceos.umanitoba.ca> From: Ian Kelly Date: Tue, 23 Dec 2014 12:23:45 -0700 Subject: Re: learning to use iterators To: Python Content-Type: multipart/alternative; boundary=001a113450cc817b11050ae71fff 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: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1419362675 news.xs4all.nl 2940 [2001:888:2000:d::a6]:41699 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82849 --001a113450cc817b11050ae71fff Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Tue, Dec 23, 2014 at 11:55 AM, Seb wrote: > > 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=C2=B9: > > >>> def n_grams(a, n): > ... z =3D (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 parentheses enclose a generator expression, which is similar to a list comprehension [1] but produce a generator, which is a type of iterator, rather than a list. In much the same way that a list comprehension can be expanded out to a for loop building a list, another way to specify a generator is by defining a function using the yield keyword. For example, this generator function is equivalent to the generator expression above: def n_gram_generator(a, n): for i in range(n): yield islice(a, i, None) [1] https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions --001a113450cc817b11050ae71fff Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On Tue, Dec 23, 2014 at 11:55 AM, Seb <spluque@gmail.com> wrote:
>
> Hi,>
> I'm fairly new to Python, and while trying to implement = a custom sliding
> window operation for a pandas Series, I came acros= s a great piece of
> code=C2=B9:
>
> >>> def n_g= rams(a, n):
> ... =C2=A0 =C2=A0 z =3D (islice(a, i, None) for i in ra= nge(n))
> ... =C2=A0 =C2=A0 return zip(*z)
> ...
>
>= ; I'm impressed at how succinctly this islice helps to build a list of<= br>> tuples with indices for all the required windows.=C2=A0 However, I&= #39;m not
> quite following what goes on in the first line of the fun= ction.
> Particulary, what do the parentheses do there?

T= he parentheses enclose a generator expression, which is similar to a list c= omprehension [1] but produce a generator, which is a type of iterator, rath= er than a list.

In much the same way that a list c= omprehension can be expanded out to a for loop building a list, another way= to specify a generator is by defining a function using the yield keyword. = For example, this generator function is equivalent to the generator express= ion above:

def n_gram_generator(a, n):
= =C2=A0 =C2=A0 for i in range(n):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 yiel= d islice(a, i, None)

--001a113450cc817b11050ae71fff--