Identifying the start of good data in a list

Terry Reedy tjreedy at udel.edu
Wed Aug 27 04:52:27 CEST 2008



Matthew Fitzgibbons wrote:
> tkpmep at hotmail.com wrote:

> 
> reHist = [0, 0, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> count = 0
> for i, d in enumerate(reHist):
>     if d == 0:
>         count = 0
>     else:
>         count += 1
>     if count == 5:
>         break
> else:
>     raise Exception("No data found")
> reHist = reHist[i-4:]
> print reHist

This is what I would have suggested, except that the 'if count' test 
should be left under the else clause, as in the original, so I consider 
it the best of the responses ;-)

I thought of the repeated slicing alternative, but it would be slightly 
slower.  However, for occasional runs, the difference would be trivial.

Worrying about what Psyco does for this problem is rather premature 
optimization.

My quarter's worth....

tjr





More information about the Python-list mailing list