It's simpler to use a defaultdict
to hold the cumulative sums of precipitations. The keys to this dict will be ordered pairs of latitude and longitude. This does the trick:
from collections import defaultdict
files = ['sunday.txt', 'monday.txt', 'tuesday.txt', 'wednesday.txt',
'thursday.txt', 'friday.txt', 'saturday.txt'
]
totals = defaultdict(float)
for fn in files:
with open(fn) as f:
for line in f.readlines():
lat, long, prec = line.split() # strings
totals[(lat, long)] += float(prec)
# See what we have:
import pprint
pprint.pprint(totals)
Here's some sample data:
monday.txt
----------
22.0 110.4 3.2
23.0 121.0 1.0
23.0 122.0 0.2
24.0 122.0 1.0
tuesday.txt
-----------
22.0 110.4 1.0
wednesday.txt
-------------
23.0 122.0 0.3
thursday.txt
------------
24.0 122.0 1.0
25.0 1.0 1.0
friday.txt
----------
24.0 122.0 1.1
saturday.txt
------------
23.0 121.0 10.5
and here's the output of the above code with these files:
{('22.0', '110.4'): 5.4,
('23.0', '121.0'): 11.5,
('23.0', '122.0'): 0.6000000000000001,
('24.0', '122.0'): 3.1,
('25.0', '1.0'): 1.0}
I haven't taken the extra step of writing the aggregated data to a file of the same format -- I'll leave that as an exercise ;)