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


Groups > comp.lang.python > #82847

learning to use iterators

From Seb <spluque@gmail.com>
Subject learning to use iterators
Date 2014-12-23 12:55 -0600
Organization Church of Emacs
Newsgroups comp.lang.python
Message-ID <mailman.17158.1419360928.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

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


Thread

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

csiph-web