I am completely new to dictionaries and I am not even sure if this is what I need to use. <br>I have a text file that I would like to run summary stats on particular months, years and climate indices (in this case the climate indices are rainfall and fire area, so not actualy climate indices at all).<br>
<br>
A text file is attached but a small sample of the file:<br> rainfall firearea<br>1972 Jan 12.7083199 0<br>1972 Feb 14.17007142 0<br>1972 Mar 14.5659302 0<br>1972 Apr 1.508517302 0<br>
1972 May 2.780009889 0<br>1972 Jun 1.609619287 0<br>1972 Jul 0.138150181 28<br>1972 Aug 0.214346148 32<br>1972 Sep 1.322102228 34747.8<br>1972 Oct 0.092663137 3655.9<br>1972 Nov 1.852276635 85.1<br>
1972 Dec 2.011206002 42959.6<br>1973 Jan 5.55704346 153.5<br>1973 Feb 12.60326356 116.2<br>1973 Mar 11.08849105 223.6<br>1973 Apr 5.864925449 2.4<br>......<br><br>I have used an example from a book (a primer on scientific programming with python) and it seems to be working (see below) but I am not sure if I have my keys etc. are set up correctly to then begin anlaysis, and even how to use the dictionaries in my analysis. For example how can I print out the year with calculated the mean 'firearea' of June-July-August for that year along with the maximum 'rainfall' for June-July-august of the same year?<br>
Any feedback will be greatly appreaciated!<br><br>infile=open('d:/yearmonthrainfire.txt','r')<br>lines=infile.readlines()<br>infile.close()<br>data={} #data[index][year]=indexvalue<br>first_line=lines[0]<br>
climateindexname=first_line.split()<br>for index in climateindexname:<br> data[index]={}<br>YEAR={}<br>MONTH={}<br><br>for line in lines[2:]:<br> words=line.split()<br> year=words[0] #years<br> YEAR[year]={}<br>
month=words[1] #months<br> MONTH[month]={}<br> values=words[2:] #values of climateindices<br> for index, v in zip(climateindexname, values):<br> if v !=' ':<br> data[index][year]=float(v)<br>
<br>print "years=", YEAR<br>print "months=", MONTH<br>print "data=", data<br>