Identifying the start of good data in a list
castironpi
castironpi at gmail.com
Thu Aug 28 14:31:48 EDT 2008
On Aug 27, 3:42 pm, George Sakkis <george.sak... at gmail.com> wrote:
> Below are two more versions that pass all the doctests: the first
> works only for lists and modifies them in place and the second works
> for arbitrary iterables:
>
> def clean_inplace(seq, good_ones=4):
> start = 0
> n = len(seq)
> while start < n:
> try: end = seq.index(0, start)
> except ValueError: end = n
> if end-start >= good_ones:
> break
> start = end+1
> del seq[:start]
>
> def clean_iter(iterable, good_ones=4):
> from itertools import chain, islice, takewhile, dropwhile
> iterator = iter(iterable)
> is_zero = float(0).__eq__
> while True:
> # consume all zeros up to the next non-zero
> iterator = dropwhile(is_zero, iterator)
> # take up to `good_ones` non-zeros
> good = list(islice(takewhile(bool,iterator), good_ones))
> if not good: # iterator exhausted
> return iterator
> if len(good) == good_ones:
> # found `good_ones` consecutive non-zeros;
> # chain them to the rest items and return them
> return chain(good, iterator)
>
> HTH,
> George
You gave me an idea-- maybe an arbitrary 'lookahead' iterable could be
useful. I haven't seen them that much on the newsgroup, but more than
once. IOW a buffered consumer. Something that you could check a
fixed number of next elements of. You might implement it as a
iterator with a __getitem__ method.
Example, unproduced:
>>> import itertools
>>> a= itertools.count( )
>>> a.next()
0
>>> a.next()
1
>>> a[ 3 ]
5
>>> a.next()
2
>>> a[ 3 ]
6
Does this make sense at all?
More information about the Python-list
mailing list