On Mar 8, 6:39 pm, John Posner <jjpos...@optimum.net> wrote: > On 3/8/2010 5:34 PM, dimitri pater - serpia wrote: > > > Hi, > > > I have two related lists: > > x = [1 ,2, 8, 5, 0, 7] > > y = ['a', 'a', 'b', 'c', 'c', 'c' ] > > > what I need is a list representing the mean value of 'a', 'b' and 'c' > > while maintaining the number of items (len): > > w = [1.5, 1.5, 8, 4, 4, 4] > > > I have looked at iter(tools) and next(), but that did not help me. I'm > > a bit stuck here, so your help is appreciated! > > Nobody expects object-orientation (or the Spanish Inquisition): >
Heh. Yep, I avoided OO for this. Seems like a functional problem. My solution is functional on the outside, imperative on the inside. You could add recursion here, but I don't think it would be as straightforward. 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:] y = y[cnt:] return result > #------------------------- > from collections import defaultdict > > class Tally: > def __init__(self, id=None): > self.id = id > self.total = 0 > self.count = 0 > > x = [1 ,2, 8, 5, 0, 7] > y = ['a', 'a', 'b', 'c', 'c', 'c'] > > # gather data > tally_dict = defaultdict(Tally) > for i in range(len(x)): > obj = tally_dict[y[i]] > obj.id = y[i] > obj.total += x[i] > obj.count += 1 > > # process data > result_list = [] > for key in sorted(tally_dict): > obj = tally_dict[key] > mean = 1.0 * obj.total / obj.count > result_list.extend([mean] * obj.count) > print result_list > #------------------------- -- http://mail.python.org/mailman/listinfo/python-list