Skip to content

Instantly share code, notes, and snippets.

@pooooch
Created October 25, 2012 07:12
Show Gist options
  • Save pooooch/3951097 to your computer and use it in GitHub Desktop.
Save pooooch/3951097 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""计算各大城市温差最大的月份
@author:linyi
"""
from __future__ import division
from sys import argv
city_month={}
result={}
script, file_name = argv
file = open(file_name)
for lines in file:
line = lines.rstrip('\n').split()
## year >= 1996
if(int(line[1][:4]) < 1996):
continue
## key = city + year + month
key = line[0] + "-" + line[1][:7]
## difference in temperature
diff = int(line[2][4:]) - int(line[3][4:])
## value = [diff sum,days]
value = city_month.get(key)
if(value):
value[0] = value[0] + diff
value[1] = value[1] + 1
else:
value = {}
value[0] = diff
value[1] = 1
city_month[key] = value
file.close()
## get the max
for key,value in city_month.items():
city = key[0:-8]
avg = value[0]/value[1]
tmp = result.get(city)
if(not tmp or tmp[1] < avg):
max = {}
max[0] = key
max[1] = avg
result[city] = max
## record the days with same avg
elif(tmp and tmp[1]==avg):
tmp[0] = tmp[0] + " " +key[-7:]
## out result
for key,value in result.items():
print key,
print value[0][value[0].index('-')+1:],
print value[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment