related lists mean value

Steve Howell showell30 at yahoo.com
Tue Mar 9 11:38:35 EST 2010


On Mar 9, 7:21 am, Steve Howell <showel... at yahoo.com> wrote:
>
> def num_dups_at_head(lst):
>     assert len(lst) > 0
>     val = lst[0]
>     i = 1
>     while i < len(lst) and lst[i] == val:
>         i += 1
>     return i
>
> def smooth(x, y):
>     result = []
>     while x:
>         cnt = num_dups_at_head(y)
>         avg = sum(x[:cnt]) * 1.0 / cnt
>         result += [avg] * cnt
>         x = x[cnt:] # expensive?
>         y = y[cnt:] # expensive?
>     return result
>

BTW I recognize that my solution would be inefficient for long lists,
unless the underlying list implementation had copy-on-write.  I'm
wondering what the easiest fix would be.  I tried a quick shot at
islice(), but the lack of len() thwarted me.






More information about the Python-list mailing list