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


Groups > comp.lang.python > #82892

Re: learning to use iterators

From Vito De Tullio <vito.detullio@gmail.com>
Subject Re: learning to use iterators
Date 2014-12-24 16:12 +0100
References <878uhyb3hq.fsf@net82.ceos.umanitoba.ca>
Newsgroups comp.lang.python
Message-ID <mailman.17185.1419433939.18130.python-list@python.org> (permalink)

Show all headers | 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