On 3/8/2010 9:43 PM, John Posner wrote:
On 3/8/2010 9:39 PM, John Posner wrote:
<snip>
obj.id = y[i] <--- statement redundant, remove it
Sorry for the thrashing! It's more correct to say that the Tally class
doesn't require an "id" attribute at all. So the code becomes:
#---------
from collections import defaultdict
class Tally:
def __init__(self):
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.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