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):

#-------------------------
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
#-------------------------

-John
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to