Average of values in a Counter
The statistics
module has a number of useful functions. These are based on having each individual data sample available for processing. In some cases, however, the data has been grouped into bins. We might have a collections.Counter
object instead of a simple list. Rather than values, we now have (value, frequency) pairs.
How can we do statistical processing on (value, frequency) pairs?
Getting ready
The general definition of the mean is the sum of all of the values divided by the number of values. It's often written like this:

We've defined some set of data, C, as a sequence of individual values, C = {c0, c1, c2, ... ,cn }, and so on. The mean of this collection, μC, is the sum of the values over the number of values, n.
There's a tiny change that helps to generalize this definition:


The value of S(C) is the sum of the values. The value of n(C) is the sum using one instead of each value. In effect, S(C) is the sum of ci1 and n(C) is the sum of ci0. We can easily...