learning to use iterators

Seb spluque at gmail.com
Fri Dec 26 16:17:44 EST 2014


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 outliers should then be replaced with linearly
interpolated values using the non-outliers.

outs = abs(x) > 0.5             # identify candidate outliers
# where(outs) would tell the location of the outliers

Here we could use something like:

from itertools import groupby
for val, g in groupby(outs):
    if outs:
        len(list(g))            # just getting sequence length here

to work with each outlier sequence, but then the indices in x are
forgotten.  They'd be needed to get the interpolates.  I feel there's
idioms that I should learn about to handle this situation more
effectively.

Cheers,

-- 
Seb




More information about the Python-list mailing list