From: Steve Howell on 9 Mar 2010 11:38 On Mar 9, 7:21 am, Steve Howell <showel...(a)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.
First
|
Prev
|
Pages: 1 2 3 Prev: Newbie question: python versions differ per user? Next: remove element with ElementTree |