Re: learning to use iterators

2014-12-26 Thread Seb
Hi again, Thanks for your input; I'm starting to use generators to some extent. Say we have a series of numbers: x = randn(100) and values beyond some criteria should be considered as outliers, but only where there's at most 3 (or some other integer) consecutive values beyond the criteria. The

Re: learning to use iterators

2014-12-25 Thread Peter Otten
Ian Kelly wrote: > On Wed, Dec 24, 2014 at 1:34 PM, Rustom Mody > wrote: >> +1 for the slice in succinct form > > Not only more succinct but also more correct. The purpose of islice is to > slice arbitrary iterables as opposed to just sequences. But this function > requires a reentrant iterable

Re: learning to use iterators

2014-12-24 Thread Ian Kelly
On Wed, Dec 24, 2014 at 1:34 PM, Rustom Mody wrote: > +1 for the slice in succinct form Not only more succinct but also more correct. The purpose of islice is to slice arbitrary iterables as opposed to just sequences. But this function requires a reentrant iterable anyway and returns garbage if y

Re: learning to use iterators

2014-12-24 Thread Rustom Mody
On Wednesday, December 24, 2014 8:42:32 PM UTC+5:30, Vito De Tullio wrote: > 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 wit

Re: learning to use iterators

2014-12-24 Thread Ben Finney
Terry Reedy writes: > On 12/23/2014 4:25 PM, Ben Finney wrote: > > To be clear: there's nothing about parentheses that produce a > > generator expression. > > Incorrect; parentheses *are* as a part of 'generator expression'. > From the doc: > generator_expression ::= "(" expression comp_for ")"

Re: learning to use iterators

2014-12-24 Thread Vito De Tullio
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 o

Re: learning to use iterators

2014-12-23 Thread Terry Reedy
On 12/23/2014 4:25 PM, Ben Finney wrote: Ian Kelly writes: On Tue, Dec 23, 2014 at 11:55 AM, Seb wrote: 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 it

Re: learning to use iterators

2014-12-23 Thread Ben Finney
Ian Kelly writes: > On Tue, Dec 23, 2014 at 11:55 AM, Seb wrote: > > 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. To b

Re: learning to use iterators

2014-12-23 Thread Terry Reedy
On 12/23/2014 1:55 PM, 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. However, I'm not quite following what goes on

Re: learning to use iterators

2014-12-23 Thread Seb
On Tue, 23 Dec 2014 12:23:45 -0700, Ian Kelly wrote: > 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 t

Re: learning to use iterators

2014-12-23 Thread Ian Kelly
On Tue, Dec 23, 2014 at 11:55 AM, Seb 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)) > ...

learning to use iterators

2014-12-23 Thread Seb
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 islic