Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82892

Re: learning to use iterators

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 <python-python-list@m.gmane.org>
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 <vito.detullio@gmail.com>
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 <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.17185.1419433939.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: learning to use iterators Vito De Tullio <vito.detullio@gmail.com> - 2014-12-24 16:12 +0100
  Re: learning to use iterators Rustom Mody <rustompmody@gmail.com> - 2014-12-24 12:34 -0800
    Re: learning to use iterators Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-24 22:36 -0700
    Re: learning to use iterators Peter Otten <__peter__@web.de> - 2014-12-25 11:34 +0100

csiph-web