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


Groups > comp.lang.python > #82847 > unrolled thread

learning to use iterators

Started bySeb <spluque@gmail.com>
First post2014-12-23 12:55 -0600
Last post2014-12-23 12:55 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  learning to use iterators Seb <spluque@gmail.com> - 2014-12-23 12:55 -0600

#82847 — learning to use iterators

FromSeb <spluque@gmail.com>
Date2014-12-23 12:55 -0600
Subjectlearning to use iterators
Message-ID<mailman.17158.1419360928.18130.python-list@python.org>
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?

Thanks,


+--- Footnotes ---+
¹ http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html#sliding-windows-n-grams-using-zip-and-iterators

-- 
Seb

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web