how to iterate over entries in a file making a dictionary with unique values and calculate an average for each value that can occur multiple times?
<p>I have a table that looks like this:</p>

<pre><code>id  value
AGA 0.211
AGA 0.433
AGA 0.123
AGH 0.002
DHI 0.063
DHI 0.193
DHI 0.004
KHI 0.543
KHI 0.064
HID 0.234
</code></pre>

<p>For each id there are sometimes different values. I want to count how many entrances there are for each id, the average and the sum of the values for each id so the outcome would be something like this:</p>

<pre><code>id      cnt   sum   av
AGA     3     0.76  0.25
AGH     1     0.002 0.002
DHI     3     0.26  0.008
KHI     2     0.607 0.304
HID     1     0.234 0.234 
</code></pre>

<p>I thouht it would be best to first make a dictionary where I count each entry, but got stuck after that, not knowing if it is best to have the value of the dictionary as an array (with the cnt, sum and av) and by then using the range of the Cnt to calculate, but could not think of ways to do that! This is how far I got:</p>

<pre><code>idDict = {}
for line in file:
    line = line.rstrip()
    f = line.split()
    id = f[0]
    idDict[id] = idDict.get(id, 0) + 1
</code></pre>

<p>But if I have already created the dictionary here with the cnt, I dont know how to iterate over each id to do the sum and av calculations :( </p>
