0
Year    Month   MeanTemp Max Temp Min Temp Total Rain(mm) Total Snow(cm)
2003    12      -0.1     9        -10.8    45             19.2
1974    1       -5.9     8.9      -20      34.3           35.6
2007    8       22.4     34.8     9.7      20.8           0
1993    7       21.7     32.5     11       87.7           0
1982    6       15.2     25.4     4        112.5          0
1940    10      7.4      22.8     -6.1     45.5           0

My data list is a tab-separated file, resembles this and goes on from 1938-2012. My issue is finding the yearly mean temperatures when all the months and years are out of order.
Any sort of beginning steps would be helpful, thanks

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223

1 Answers1

0

Use a dictionary with the year as the key and the temp and a counter as the value (as a list). If the year isn't found add an entry with the mean temp and the counter at 1. If the year is already there add the temperature to the existing temp and increment the counter.

The rest should be easy. Note this gives you the average mean temperature for the years. If you want the true mean it will be a bit more involved.

mister270
  • 366
  • 1
  • 15
  • You don't need a separate counter per year -- it's implicitly the length of list. Might also want to use a `defaultdict` so adding the first entry is no longer a special case. – martineau Jun 13 '14 at 16:32
  • Very good. Concatenate each temperature to the list and divide by the length. Also I misspoke (typed?), mean and average are the same thing. It's the median that requires a slightly different calculation. – mister270 Jun 13 '14 at 17:00