Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82849
| 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 | <ian.g.kelly@gmail.com> |
| 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 <ian.g.kelly@gmail.com> |
| Date | Tue, 23 Dec 2014 12:23:45 -0700 |
| Subject | Re: learning to use iterators |
| To | Python <python-list@python.org> |
| 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 <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.17160.1419362675.18130.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
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 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?
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: learning to use iterators Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-23 12:23 -0700
csiph-web