Python 3.4 calculating the mode,median reading through a file
<p>I was wondering if there was another way to code 
At the core of this problem is the easiest way to solve this problem is to read a file and save the values in the list. Where then you'd have:    </p>

<pre><code>a = [1,2,3,4,5,6,1,1,1,1]
import statistics
listMode = statistics.mode(a) # median, average, etc...
</code></pre>

<p>I was wondering instead of having to save these values in <code>a</code> (so memory as it can be quite large), whether I could calculate the mode on the fly as I read the file and update a single value everytime I read a new line i.e. incrementally calculate mode,median and average. So that at the end I'd have <code>a = [mode,median,average]</code>. </p>

<p>If the set of input numbers comes from a reasonably small universe of values, as in your example, you could use a <code>Counter</code> to count how many of each value you see as they pass by. From that <code>Counter</code> you can get the mode easily, and the median with a little work. Calculating the average on the fly is easy, doesn't need the <code>Counter</code>: just keep a running total and a running count.</p>

python python-3.x